如何在jstl中使用正则表达式?
How to use regex expression in jstl?
我在 tld 中定义了一个函数 file.And 它可以工作 well.But 如果我想从 jsp 传递一个参数,它显示 wrong.What 的错误是吗?
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();
}
现在,我想改成这样:
public static boolean matches(String pattern, CharSequence str) {
return Pattern.compile(pattern).matcher(str).matches();
}
而在 jsp 中,我是这样传递的:
<c:if test="${mapping:matches('^(((153|133)\d{8})|((1704|1707)\d{7}))$',data.phone)}">
This is a phone
</c:if>
错误 shows:Within 仅引用字符串 []、['] 和 ["] 可能会用 [] 转义。
您应该使用 \\d
而不是 \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.
我在 tld 中定义了一个函数 file.And 它可以工作 well.But 如果我想从 jsp 传递一个参数,它显示 wrong.What 的错误是吗?
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();
}
现在,我想改成这样:
public static boolean matches(String pattern, CharSequence str) {
return Pattern.compile(pattern).matcher(str).matches();
}
而在 jsp 中,我是这样传递的:
<c:if test="${mapping:matches('^(((153|133)\d{8})|((1704|1707)\d{7}))$',data.phone)}">
This is a phone
</c:if>
错误 shows:Within 仅引用字符串 []、['] 和 ["] 可能会用 [] 转义。
您应该使用 \\d
而不是 \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.