StringEscapeUtils 不转义 {
我有以下字符串:
String str = "{% assign foo = values %}.{{ foo[0] }}."
我正在尝试将其编译为模式:
Pattern p = Pattern.compile(StringEscapeUtils.escapeJava(str));
但编译失败并出现“非法重复”错误,我猜测这是由于 '{' 字符没有被转义。
怎样才能正确逃脱呢?最好不要在每个字符前面添加“\\”。
I have the following string:
String str = "{% assign foo = values %}.{{ foo[0] }}."
And I'm trying to compile it as a pattern with:
Pattern p = Pattern.compile(StringEscapeUtils.escapeJava(str));
but the compilation fails with the "Illegal repetition" error which I'm guessing is due to the '{' char not being escaped.
How can I escape it properly? Preferably without adding "\\" in front of every character.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

发布评论
评论(3)
~没有更多了~
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
您不需要
StringEscapeUtils.escapeJava()
您需要Pattern.quote()
。You do not want
StringEscapeUtils.escapeJava()
You wantPattern.quote()
.