【发布时间】:2018-05-28 06:41:06
【问题描述】:
输入
example("This is tes't")
example('This is the tes\"t')
输出应该是
This is tes't
This is the tes"t
代码
String text = "example(\"This is tes't\")";
//String text = "$.i18nMessage('This is the tes\"t\')";
final String quoteRegex = "example.*?(\".*?\")?('.*?')?";
Matcher matcher0 = Pattern.compile(quoteRegex).matcher(text);
while (matcher0.find()) {
System.out.println(matcher0.group(1));
System.out.println(matcher0.group(2));
}
我看到输出为
null
null
虽然当我使用正则表达式 example.*?(\".*?\") 它返回 This is tes't 而当我使用 example.*?('.*?') 它返回
This is the tes"t 但当我将两者与 example.*?(\".*?\")?('.*?')? 结合使用时,它返回 null 。为什么?
【问题讨论】:
-
如果您打算匹配整个字符串,请使用
matcher0.matches()。但是,您正在寻找类似example\\((([\"'])(?:(?!\\2).)*\\2)\\) -
我不明白你想在这里匹配什么。您能否向我们展示示例输入和您想要的匹配项?
-
@TimBiegeleisen 请看我的更新
-
@WiktorStribiżew 我想知道我的正则表达式中的问题,因为第一和第二个捕获组可以独立工作,但是当我将它们组合起来并使用
?使其成为可选时,它不起作用? -
您的正则表达式匹配like this。或者只是
example,或者如果引用的刺痛紧随其后,那些被捕获的。发生这种情况是因为正则表达式末尾的所有模式都是可选的。您可能至少尝试过(\".*?\"|'.*?')。