Sasha Sasha - 6 months ago 10
SQL Question

Regular Expression extracted from DB not matching the value

In short:

String regExp = "\\w{3}-\\d{3}";
System.out.println("REF-123".matches(regExp)); // IS TRUE

// Exact same regular expression extracted from the database: \\w{3}-\\d{3}
regExp = resultSet.getString("regex");
System.out.println("REF-123".matches(regExp)); // IS FALSE


Any help is highly appreciated.

Answer

Backslash symbol (\) in string constant is for escaping purposes.

For example

System.out.println("A\\B");

Will print A\B. In your database string must be \w{3}-\d{3}

Comments