Ramson
Ramson

Reputation: 229

Regular expression is working in standalone but not in web applicaiton

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

Answers (1)

Pshemo
Pshemo

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:

  • index of space is 32
  • index of ' is 39
  • and index of % 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

  • escape it with \- (written as string "\\-")
  • or place it somewhere where it can't be treated as range indicator like:
    • at start of character class [-foo]
    • at end of character class [bar-]
    • after already created range [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

Related Questions