我的值是这样的:

"Foo Bar" "Another Value" something else

什么正则表达式将返回括在引号中的值(例如Foo Bar和Another Value)?


当前回答

如果你试图查找只有特定后缀的字符串,比如点语法,你可以尝试这样做:

\"([^\"]*?[^\"]*?)\". 本地化

其中。本地化是后缀。

例子:

打印(“这是我需要归还的东西”。本地化+“所以是这个”。本地化+ "but this is not")

它会捕捉到“这是我需要归还的东西”。本地化和“这也是”。本地化但不是“but this is not”。

其他回答

很晚才回答,却喜欢回答

(\"[\w\s]+\")

http://regex101.com/r/cB0kB8/1

以上所有的答案都很好....除了他们不支持所有的unicode字符!at ECMA Script (Javascript)

如果你是Node用户,你可能想要支持所有unicode字符的可接受答案的修改版本:

/(?<=((?<=[\s,.:;"']|^)["']))(?:(?=(\\?))\2.)*?(?=\1)/gmu

试着在这里。

我喜欢《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

对于Microsoft VBA编码器子集的补充回答,只有一个人使用Microsoft VBScript正则表达式5.5库,这给出了以下代码

Sub TestRegularExpression()

    Dim oRE As VBScript_RegExp_55.RegExp    '* Tools->References: Microsoft VBScript Regular Expressions 5.5
    Set oRE = New VBScript_RegExp_55.RegExp

    oRE.Pattern = """([^""]*)"""


    oRE.Global = True

    Dim sTest As String
    sTest = """Foo Bar"" ""Another Value"" something else"

    Debug.Assert oRE.test(sTest)

    Dim oMatchCol As VBScript_RegExp_55.MatchCollection
    Set oMatchCol = oRE.Execute(sTest)
    Debug.Assert oMatchCol.Count = 2

    Dim oMatch As Match
    For Each oMatch In oMatchCol
        Debug.Print oMatch.SubMatches(0)

    Next oMatch

End Sub

接受的答案的RegEx返回值,包括它们的寻源引号:“Foo Bar”和“Another Value”作为匹配。

下面是RegEx,它只返回引号之间的值(正如提问者所要求的那样):

仅使用双引号(使用捕获组#1的值):

"(.*?[^\\])"

仅使用单引号(使用捕获组#1的值):

'(.*?[^\\])'

Both(使用捕获组#2的值):

([']) "(. *? 1 \ [^ \ \])

-

全部支持转义和嵌套引号。