I have a requirement to ensure that the string has only alphanumeric or a set of characters like + ( ) , ‘ . – =
I tried like
String regex = "([a-zA-Z0-9]+)|([\\'|\\()|\\+|\\,|\\-|\\.|\\="]+)"; System.out.println("Regex : " + regex); Pattern pattern = Pattern.compile(regex); Matcher matcher = pattern.matcher(str); if (matcher.matches()) { System.out.println("Match"); } else { System.out.println("Not Matching"); }
Bt it is not working, Can anyone assist me please
Thanks in advance
It looks like it will not match a whole string containing a mix of alphanumerics & symbols because of the OR in the middle.
e.g. it wont match abcABC()+, yet will match abcABC & will match ()+
Try:
([a-zA-Z0-9\\'\\(\\+\\)\\,\\-\\.\\=]+)
Hope this helps!
No comments:
Post a Comment