0

I need to check strings to see if they only contain "Nonalphanumeric characters". basically I can have a string that is "!!!hi" and this would be okay, because it contains alphanumeric characters as well as non alphanumeric, but a line that is just "@@#!" this would be deleted. It seems like I would need some type of "Contains" that's not the normal java contains.

Here is what I have tried.

if(line.matches("[^a-zA-Z\\d\\s:]")){
   line = line;
} else {
   line = line.replace(line, "");
}
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
dmc94
  • 536
  • 1
  • 5
  • 16
  • 1
    `line.replace(line, "")` will remove the entire string. Why not just `line = ""`? – OneCricketeer Jul 27 '20 at 17:18
  • @dmc94 please specify your question in a clearer way. You have deviation even from the original question, and you didn't like what was the working regex. What exactly you are looking for? – Giorgi Tsiklauri Jul 27 '20 at 17:31
  • @GiorgiTsiklauri I disagree, you didnt read the full question. I mentioned "!!hi" would stay in the string while "@@#!" wouldn't. – dmc94 Jul 27 '20 at 18:44
  • @dmc94 how do you know that I didn't read it? I did, and that's why I've commented on it. – Giorgi Tsiklauri Jul 27 '20 at 18:52
  • @GiorgiTsiklauri I gave you a concrete example in the middle of the post which was ignored when you posted your answer. Your answer did not work. Your answer did not answer the question, or match the expected input/output. I can only assume you did not read it or you chose to give a wrong answer based on the information provided to you. – dmc94 Jul 27 '20 at 18:57
  • @dmc94 next time you post some question, make sure you don't include two contradictory statements. Just make your English clear. My answer **says** for what does it work. – Giorgi Tsiklauri Jul 27 '20 at 18:58
  • Does this answer your question? [Regex, every non-alphanumeric character except white space or colon](https://stackoverflow.com/questions/6053541/regex-every-non-alphanumeric-character-except-white-space-or-colon) – Giorgi Tsiklauri Aug 26 '20 at 10:15

2 Answers2

1

If you just want to see if it contains an alphanumeric, then you can find() it

import java.util.regex.Matcher;
import java.util.regex.Pattern;


    public String clean(String line) {
        Pattern p = Pattern.compile("\w"); 

        Matcher m = p.matcher(line);
        if (m.find()) {
          return line; // found any character or digit, keep the line
        }
        return ""; // else return nothing
    }
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
-1

Use this regex:

if(!line.matches("^[a-zA-Z0-9!]*$"))

to check if there is any non-alphanumeric character in your string line:

Giorgi Tsiklauri
  • 9,715
  • 8
  • 45
  • 66
  • 1
    correct, but if there is a string that contains alphanumeric AND nonalphanumeric this does not work. "#$hello" should not be deleted but "#$#" should. This regex would delete the first case – dmc94 Jul 27 '20 at 17:24
  • `I need to check strings to see if they only contain "Nonalphanumeric characters"` - isn't that your question? My answer will even include `!`. So, it complies with all your requirement. If you're asking for something else, then be kind and specify what you exactly want to achieve. – Giorgi Tsiklauri Jul 27 '20 at 17:25
  • @dmc94 Regexes do not "delete" – OneCricketeer Jul 27 '20 at 17:28
  • Where did you get `!`? What about the other symbols? – OneCricketeer Jul 27 '20 at 17:32
  • @OneCricketeer from the OP's question, which actually is very vague and unclear. `basically I can have a string that is "!!!hi"` – Giorgi Tsiklauri Jul 27 '20 at 17:33
  • The question simply stated that if the string is all symbols, then it should become an empty string – OneCricketeer Jul 27 '20 at 17:35
  • @OneCricketeer seriously?? pay attention to the question: 1. `I need to check strings to see if they only contain "Nonalphanumeric characters"`; 2. `basically I can have a string that is "!!!hi" and this would be okay, because it contains alphanumeric characters as well as non alphanumeric.` This is contradictory to each other. – Giorgi Tsiklauri Jul 27 '20 at 17:37
  • Keyword: If they ONLY contain – OneCricketeer Jul 27 '20 at 17:44