我无法从字符串转义双美元用于正则表达式函数模式/匹配器。转义美元
这是字符串的一部分:
WHERE oid_2 = $$test$$ || oid_2 = $$test2$$
,这是我一直试图让解附近最接近代码:
List<String> strList = new ArrayList<String>();
Pattern pattern = Pattern.compile("\$\$.*?\$\$");
log.debug("PATTERN: "+pattern)
Matcher matcher = pattern.matcher(queryText);
while (matcher.find()) {
strList.add(matcher.group());
}
log.debug(strList)
这是调试输出我得到
- PATTERN: $$.*?$$
- []
所以模式实际上是正确的,但在字符串中找不到占位符。
作为一个测试,我试图用“XXtestXX”替换“$$ test $$”,并且一切正常。我错过了什么?我试过“/ $”字符串,“\\”但仍然没有解决方案。
请参阅https://ideone.com/Olm30x –
“\\ $ \\ $。*?\\ $ \\ $”表示:美元符号后的非法字符串正文字符;解决方案:要么转义字面美元符号“\ $ 5”,要么将值表达式“$ {5}”括在第7行第38列中。Pattern pattern = Pattern.compile(“\\ $ \\ $。*?\\ $ \\ $“)^ 1错误 – GiLA3
虽然使用Slashy字符串它的工作原理!非常感谢,我确信我已经尝试了这个解决方案,但不知怎的,我没有,再次感谢! – GiLA3