我的值是这样的:
"Foo Bar" "Another Value" something else
什么正则表达式将返回括在引号中的值(例如Foo Bar和Another Value)?
我的值是这样的:
"Foo Bar" "Another Value" something else
什么正则表达式将返回括在引号中的值(例如Foo Bar和Another Value)?
当前回答
一般来说,下面的正则表达式片段就是你要找的:
"(.*?)"
这使用了非贪婪的*?运算符来捕获下一个双引号之前的所有内容。然后,使用特定于语言的机制提取匹配的文本。
在Python中,你可以这样做:
>>> import re
>>> string = '"Foo Bar" "Another Value"'
>>> print re.findall(r'"(.*?)"', string)
['Foo Bar', 'Another Value']
其他回答
我喜欢《Axeman》更广阔的版本,但也遇到了一些问题(游戏邦注:例如它并不匹配
foo "string \\ string" bar
or
foo "string1" bar "string2"
所以我试着修正它:
# opening quote
(["'])
(
# repeat (non-greedy, so we don't span multiple strings)
(?:
# anything, except not the opening quote, and not
# a backslash, which are handled separately.
(?!\1)[^\\]
|
# consume any double backslash (unnecessary?)
(?:\\\\)*
|
# Allow backslash to escape characters
\\.
)*?
)
# same character as opening quote
\1
特别的是,这些答案都不会产生一个正则表达式,其中返回的匹配是所要求的引号内的文本。MA-Madden尝试了,但只获得了内部匹配,而不是整个匹配。一种实际的方法是:
(?<=(["']\b))(?:(?=(\\?))\2.)*?(?=\1)
这方面的例子可以在这个演示https://regex101.com/r/Hbj8aP/1中看到
The key here is the the positive lookbehind at the start (the ?<= ) and the positive lookahead at the end (the ?=). The lookbehind is looking behind the current character to check for a quote, if found then start from there and then the lookahead is checking the character ahead for a quote and if found stop on that character. The lookbehind group (the ["']) is wrapped in brackets to create a group for whichever quote was found at the start, this is then used at the end lookahead (?=\1) to make sure it only stops when it finds the corresponding quote.
唯一的另一个复杂之处在于,由于前向查询实际上并不使用结束引号,它将被开始后向查询再次找到,这将导致匹配同一行上结束引号和开始引号之间的文本。在开头引用(["']\b)上加上一个单词边界有助于解决这个问题,尽管理想情况下我想跳过前瞻,但我认为这是不可能的。中间允许转义字符的部分直接取自亚当的回答。
echo 'junk "Foo Bar" not empty one "" this "but this" and this neither' | sed 's/[^\"]*\"\([^\"]*\)\"[^\"]*/>\1</g'
这将导致:>Foo Bar<><>但这<
在这里,为了清晰起见,我显示了><'s之间的结果字符串,也使用了这个sed命令的非贪婪版本,我们首先抛出' 's之前和之后的垃圾,然后用' ' 's之间的部分替换它,并用><'s包围它。
模式(["'])(?:(?=(\\?))\2.)*?上面的\1完成了这项工作,但我担心它的性能(它不差,但可以更好)。我的比它快20%。
模式“(.*?)”是不完整的。我对阅读这篇文章的人的建议是不要使用它!!
例如,它不能捕获很多字符串(如果需要,我可以提供一个详尽的测试用例),如下所示:
$string = '你好吗?我很好,谢谢你;
其余的都和上面那个一样“好”。
如果你真的关心性能和精度,那么就从下面的开始:
/(['"])((\\\ 1 |。)* ?)\ 1 /通用
在我的测试中,它涵盖了我遇到的每一个字符串,但如果你发现一些不工作的东西,我很乐意为你更新它。
在在线正则表达式测试器中检查我的模式。
我一直在使用以下方法并取得了巨大的成功:
(["'])(?:(?=(\\?))\2.)*?\1
它还支持嵌套引号。
对于那些想要更深入地解释这是如何工作的人,这里是用户ephemerent的解释:
([""'])匹配引号;((?=(\\?))\2.)如果存在反斜杠,吞噬它,无论是否发生,匹配一个字符;* ?匹配多次(非贪婪,如不吃结尾引号);\1匹配相同的报价,是用于开幕。