Java 正则表达式:意外字符:'('
我正在使用processing 1.0.6,它在java 1.6上运行。我有以下代码:
Pattern pattern = Pattern.compile("func\((.*),(\-?[0-9\.]*),(\-?[0-9\.]*),(\-?[0-9\.]*)\)");
但它给出了错误:
unexpected char: '('
并突出显示了我上面粘贴的行。如果我将有问题的 \(
更改为 #
等另一个字符,它会抱怨 \-
说 unexpected char: '-'我应该能够在正则表达式引擎中使用文字,并且它可以在 C# 中工作!
I am using processing 1.0.6, which runs on java 1.6. I have the following code:
Pattern pattern = Pattern.compile("func\((.*),(\-?[0-9\.]*),(\-?[0-9\.]*),(\-?[0-9\.]*)\)");
but it gives the error:
unexpected char: '('
and highlights the line I pasted above. If I change the offending \(
to another character like #
, it complains of the \-
saying unexpected char: '-'
. I should be able to use literals in a regex engine and it works in C#!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您也需要转义反斜杠:
该字符串表达式将被计算为:
这就是您想要的。
You need to escape the backslashes too:
That string expression will the be evaluated to:
And that’s what you wanted.
在 Java 中,你必须在字符串前面加上 \ 来转义 \,所以
应该可以。
You have to escape \ in strings in Java by preceding it with a \, so
should work.
在Java中'('和'-'不是需要转义的字符。因此'('意味着'\'没有任何需要转义的字符,这就是发生错误的原因。解决这个问题的解决方案是反斜杠的escepe :
如果您需要在 Java 和 C# 中使用 RegEx,也许您应该编写一个从一种语言交换到另一种语言的程序。
In Java '(' and '-' is not an escape-required character. So '(' means '\' without any escape-required character that is why the error occur. The solution to this is the escepe that back-slash too:
If you need to use RegEx in both Java and C#, perhaps you should write a program that swap from one to another.
我认为你应该将
"\"
替换为"\\"
I think you should replace
"\"
with"\\"