Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

validate string with regex on android

I want that my string didn't contain *,; and $. I use this code

private static boolean IsMatch(String s, String pattern) {
         try {
             Pattern patt = Pattern.compile(pattern);
             Matcher matcher = patt.matcher(s);
             return matcher.matches();
         } catch (RuntimeException e) {
           return false;
         }  
}



String regex ="[^*;$]";
System.out.println(IsMatch(url,regex));

But this method return always false. Can any one tell me what's the problem

like image 205
foufi Avatar asked Apr 13 '11 08:04

foufi


People also ask

What is regex Android studio?

Regular Expression basically defines a search pattern, pattern matching, or string matching. It is present in java. util. regex package.


1 Answers

Try using [^*;$]* for your regex. It'll return true if your string doesn't contain any of *, ; and $. I'm assuming you want your regex to match strings that don't contain any of those characters since you're already using ^ inside [ and ].

like image 116
WhiteFang34 Avatar answered Sep 28 '22 07:09

WhiteFang34