Reputation: 229
Hi I have a regular expression which is working fine if I run as a standalone java application and returning true:
public class Example {
private static final String VALID_ADVISOR_NAME_FORMAT =
"[a-zA-Z" + " " + "-" + "'" + "&" + "(" + ")" + "]";
public static void main(String[] args) {
System.out.println(isInvalidAdvisorName("%%%%%"));
}
public static boolean isInvalidAdvisorName(String name) {
return !name.matches(VALID_ADVISOR_NAME_FORMAT);
}
}
But if I use the same method in my web application (runs in Tomcat), the output is false
. Can anyone tell me why "%%%"
is getting considered as invalid in standalone app but valid in web app that runs in tomcat?
Upvotes: 0
Views: 68
Reputation: 124265
-
represents range of characters in Unicode table. So just like a-z
represents all characters which are between a
and z
, regex like [ -']
(you are creating it via " " + "-" + "'"
) represents all characters between space
and '
.
Now lets take a look at indexes of some characters in Unicode table:
32
'
is 39%
is 37
so %
is placed between space
and '
so it is accepted by regex [ -']
as valid character.
To make -
literal inside [ ]
you need to either
\-
(written as string "\\-"
) [-foo]
[bar-]
[a-z-1]
where a-z
will represent range and -
placed right after them (and 1
) will be treated as literals.So you can try with this pattern (and drop the concatenation part, it actually makes your regex harder to read and spot mistakes).
VALID_ADVISOR_NAME_FORMAT = "[a-zA-Z \\-'&()]";
Also if you want your pattern to accept more than one characters in described range add +
after character class (+
represents one or more quantified).
VALID_ADVISOR_NAME_FORMAT = "[a-zA-Z \\-'&()]+";
Upvotes: 1