I have a problem with the % and / characters in Java regex. The following example will illustrate my issue:
Pattern pattern = Pattern.compile("^[a-z]*[/%]$");
Matcher m = pattern.matcher("a%/");
System.out.println(m.find());
It prints “false” when I expect it to be “true”. The % and / sign shouldn’t have to be escaped but even if I do it still dosn’t work.
So my question is simply why?
^[a-z]*[/%]$matches zero or more lower case letters followed by one character which can be either/or%– to allow multiple characters, use+stands for one or more; use*for zero or more.If you didn’t have
$at the end of the regex, it would have matcheda%in the stringa%/.$matches end of line.