正确匹配 Java 字符串文字
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2958825/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Properly match a Java string literal
提问by Tiago Veloso
I am looking for a Regular expression to match string literals in Java source code.
我正在寻找一个正则表达式来匹配 Java 源代码中的字符串文字。
Is it possible?
是否可以?
private String Foo = "A potato";
private String Bar = "A \"car\"";
My intent is to replace all strings within another string with something else. Using:
我的意图是用其他东西替换另一个字符串中的所有字符串。使用:
String A = "I went to the store to buy a \"coke\"";
String B = A.replaceAll(REGEX,"Pepsi");
Something like this.
像这样的东西。
采纳答案by Wangnick
Ok. So what you want is to search, within a String, for a sequence of characters starting and ending with double-quotes?
行。那么您想要的是在字符串中搜索以双引号开头和结尾的字符序列?
String bar = "A \"car\"";
Pattern string = Pattern.compile("\".*?\"");
Matcher matcher = string.matcher(bar);
String result = matcher.replaceAll("\"bicycle\"");
Note the non-greedy .*?pattern.
注意非贪婪.*?模式。
回答by Hachi
this regex can handle double quotes as well (NOTE: perl extended syntax):
这个正则表达式也可以处理双引号(注意:perl 扩展语法):
"
[^\"]*
(?:
(?:\\)*
(?:
\
"
[^\"]*
)?
)*
"
it defines that each " has to have an odd amount of escaping \ before it
它定义了每个 " 在它之前必须有奇数的转义 \
maybe it's possible to beautify this a bit, but it works in this form
也许可以稍微美化一下,但它以这种形式工作
回答by Uri
You can look at different parser generators for Java, and their regular expression for the StringLiteral grammar element.
您可以查看 Java 的不同解析器生成器,以及它们的 StringLiteral 语法元素的正则表达式。
Here is an example from ANTLR:
StringLiteral
: '"' ( EscapeSequence | ~('\'|'"') )* '"'
;
回答by Richard H
You don't say what tool you're using to do your finding (perl? sed? text editor ctrl-F etc etc). But a general regex would be:
你没有说你用什么工具来做你的发现(perl?sed?文本编辑器ctrl-F等)。但是一般的正则表达式是:
\".*?\"
Edit:this is a quick & dirty answer, and doesn't cope with escaped quotes, comments etc
编辑:这是一个快速而肮脏的答案,不处理转义的引号、评论等

