Search code examples
javaregexjspjstl

How to use regex expression in jstl?


I have defined a function in tld file.And it works well.But if I want to pass a parameter from the jsp,it show wrong.What's wrong with it?

 public static boolean matcheTelcom(CharSequence str) { 
    public static String regTelcom="^(((153|133)\\d{8})|((1704|1707)\\d{7}))$";
     return Pattern.compile(Constant.regTelcom).matcher(str).matches();
      }

Now,I want to change it to this:

public static boolean matches(String pattern, CharSequence str) {
        return Pattern.compile(pattern).matcher(str).matches();
      }

And in the jsp,I pass it like this:

 <c:if test="${mapping:matches('^(((153|133)\\d{8})|((1704|1707)\\d{7}))$',data.phone)}">  
     This is a phone
  </c:if>

The error shows:Within a quoted String only [], ['] and ["] may be escaped with [].


Solution

  • You should use \\\\d instead of \\d.

    The resean why you shoud use four \ because in java language \ have transferred meaning.you should use \\ means \,so does the same in regex expression.