-1

I'm trying to create a function that's returns a boolean true when a find least one illegal character.

I test the following function in JAVA 8 and runs ok but in android return false even with an illegal character inside the string.

First I try this but fail on both environments.

boolean HaveIllegalChars(String cmd)
{
    String IllegalChars = "[^*;<>]";

    boolean result = cmd.matches(IllegalChars);
    return result;
}

Then I read this post Regex doesn't work in String.matches() and change the regex expression and start to works on JAVA; so I paste the same string in Android app but when I run the app in a real device didn't work as expected.

boolean HaveIllegalChars(String cmd)
{
    String IllegalChars = ".*[^*;<>].*";

    boolean result = cmd.matches(IllegalChars);
    return result;
}

Any idea what's happening?

Edit:

I tried the following strings

String illegalStr= "*mmm"; 
String illegalStr1= "<mmm";
String illegalStr2= ";mmm";
String okStr ="!ABC1";

In JAVA illegalStr returns true(which is what i want) in Android false.

In JAVA illegalStr1 returns true(which is what i want) in Android false.

In JAVA illegalStr2 returns true(which is what i want) in Android false.

In JAVA okStr returns false(which is what i want) in Android false.

Community
  • 1
  • 1
E_Blue
  • 1,021
  • 1
  • 20
  • 45
  • 2
    Please explain, **in detail**, what "didn't work as expected" means. For example, you wrote "in JAVA 8 and runs ok but in android return false even with an illegal character inside the string". What is an input value for `cmd` that works on the Java 8 JVM and does not work in Android? – CommonsWare Nov 13 '16 at 23:43
  • your regex means "1 character that is not one of `*;<>`". `matches` means that the entire String must match the regex. – njzk2 Nov 13 '16 at 23:49
  • 2
    Perhaps you want `!cmd.matches("^[^*;<>]*$")` – 4castle Nov 13 '16 at 23:50
  • @4castle - when using `matches`, `^` and `$` are not necessary, as that method already matches the entire string – BackSlash Nov 13 '16 at 23:51
  • @njzk2 I'm not an expert but according to the web page regex101.com "matches a single character in the list *;<>" – E_Blue Nov 13 '16 at 23:58
  • @BackSlash Yes, I know that, but I always put them there so that my intent is clear. – 4castle Nov 13 '16 at 23:59
  • @CommonsWare I added info to the original post as you requested. – E_Blue Nov 14 '16 at 00:06
  • @4castle That's work as I need. +1 – E_Blue Nov 14 '16 at 00:19
  • 1
    There's something in your Android code causing the matcher not to get the string you think it is. Of course without a complete example, there's no way to verify. Anyway, you're going about this in a truly weird fashion. Use `Pattern.compile("[*;<>]").matcher(cmd).find()`. Except compile the pattern once and save it in a static member. – Gene Nov 14 '16 at 00:25
  • @Gene Why is a weird fashion? :P . I think as you, something is changing the regex or the string that I'm trying to check. The whole android app have two editText and a button; the user must enter a text without illegal chars and a four digits HEX number in the other editText; both parameters generates a key when the user push the button and the key is copied to the clipboard. – E_Blue Nov 14 '16 at 00:37
  • @E_Blue, `^` at the begining of `[]` inverts the character class. – njzk2 Nov 14 '16 at 02:09
  • "In JAVA illegalStr returns true" doubtful. I just tried it: https://ideone.com/SiCJgZ and the result is, has should be, false for each test case. (trivially because your strings are more than 1 character each) – njzk2 Nov 14 '16 at 02:15
  • @njzk2 I was talking about the second code. Before the first code I said "First I try this but fail on both environments.". Please re read. – E_Blue Nov 14 '16 at 02:50
  • @njzk2 I don't understand why this question is an EXACT duplicate. I based this question on the "duplicate". – E_Blue Nov 14 '16 at 02:54
  • alright, that makes sense. Reopening. – njzk2 Nov 14 '16 at 03:00
  • @E_Blue: second code returns true every time, which makes perfect sense. your regex matches the string as long as at least one character is not one of the forbidden ones. an example that would not match would be `";>>*<<;"`: https://ideone.com/vIcZMX – njzk2 Nov 14 '16 at 03:03
  • Possible duplicate of [Java function to return if string contains illegal characters](http://stackoverflow.com/questions/14635391/java-function-to-return-if-string-contains-illegal-characters) – OneCricketeer Nov 14 '16 at 03:07

1 Answers1

0

To tell if a string contains any of the characters that you are searching for, you can do any of:

// at least one of the characters is one of those
return cmd.matches(".*[*;<>].*");

// all the characters are none of those, then invert
return !cmd.matches("[^*;<>]*");

// Search for one character using Pattern
return Pattern.compile("[*;<>]").matcher(cmd).find();

// Same as second one
return !Pattern.compile("[^*;<>]*").matcher(cmd).matches();
njzk2
  • 38,969
  • 7
  • 69
  • 107