我的值是这样的:
"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']
其他回答
string = "\" foo bar\" \"loloo\""
print re.findall(r'"(.*?)"',string)
试试这个,就像一个魅力!!
\表示跳过字符
这个版本
转义引号的说明 控制回溯 /(["'])((?:(?!\ 1 )[^\\]|(?:\\\\)*\\[^\\])*)\ 1 /
我喜欢《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
很晚才回答,却喜欢回答
(\"[\w\s]+\")
http://regex101.com/r/cB0kB8/1
以上所有的答案都很好....除了他们不支持所有的unicode字符!at ECMA Script (Javascript)
如果你是Node用户,你可能想要支持所有unicode字符的可接受答案的修改版本:
/(?<=((?<=[\s,.:;"']|^)["']))(?:(?=(\\?))\2.)*?(?=\1)/gmu
试着在这里。