正则表达式仅匹配引号内的空格

时间:2022-01-21 16:51:39

I need to match any space within double quotes ONLY - not outside. I've tried a few things, but none of them work.

我需要匹配双引号内的任何空格 - 不在外面。我尝试了一些东西,但没有一个能奏效。

  • [^"]\s[^"] - matches spaces outside of quotes
  • [^“] \ s [^”] - 匹配引号之外的空格

  • [^"] [^"] - see above
  • [^“] [^”] - 见上文

  • \s+ - see above
  • \ s + - 见上文

For example I want to match "hello world" but not "helloworld" and not hello world (without quotes). I will specifically be using this regex inside of Visual Studio via the Find feature.

例如,我想匹配“hello world”而不是“helloworld”而不是hello world(没有引号)。我将通过Find功能专门在Visual Studio中使用此正则表达式。

1 个解决方案

#1


0  

With .net and pcre regex engines, you can use the \G feature that matches the position after a successful match or the start of the string to build a pattern that returns contiguous occurrences from the start of the string:

使用.net和pcre正则表达式引擎,您可以使用匹配成功匹配后的位置或字符串开头的\ G功能来构建从字符串开头返回连续出现的模式:

((?:\G(?!\A)|\A[^"]*")[^\s"]*)\s([^\s"]*"[^"]*")?

example for a replacement with #: demo

使用#:demo替换的示例

pattern details:

( # capture group 1
    (?: # two possible beginning
        \G(?!\A) # contiguous to a previous match
      |          # OR
        \A[^"]*" # start of the string and reach the first quote
    )   # at this point you are sure to be inside quotes    
    [^\s"]* # all that isn't a white-space or a quote
)
\s # the white-space
([^\s"]*"[^"]*")? # optional capture group 2: useful for the last quoted white-space
                  # since it reaches an eventual next quoted part. 

Notice: with the .net regex engine you can also use the lookbehind to test if the number of quotes before a space is even or odd, but this way isn't efficient. (same thing for a lookahead that checks remaining quotes until the end, but in addition this approach may be wrong if the quotes aren't balanced).

注意:使用.net正则表达式引擎,您还可以使用lookbehind来测试空格前的引号数是偶数还是奇数,但这种方式效率不高。 (同样的事情是先行检查剩余的报价直到结束,但另外如果报价不平衡,这种方法可能是错误的)。

#1


0  

With .net and pcre regex engines, you can use the \G feature that matches the position after a successful match or the start of the string to build a pattern that returns contiguous occurrences from the start of the string:

使用.net和pcre正则表达式引擎,您可以使用匹配成功匹配后的位置或字符串开头的\ G功能来构建从字符串开头返回连续出现的模式:

((?:\G(?!\A)|\A[^"]*")[^\s"]*)\s([^\s"]*"[^"]*")?

example for a replacement with #: demo

使用#:demo替换的示例

pattern details:

( # capture group 1
    (?: # two possible beginning
        \G(?!\A) # contiguous to a previous match
      |          # OR
        \A[^"]*" # start of the string and reach the first quote
    )   # at this point you are sure to be inside quotes    
    [^\s"]* # all that isn't a white-space or a quote
)
\s # the white-space
([^\s"]*"[^"]*")? # optional capture group 2: useful for the last quoted white-space
                  # since it reaches an eventual next quoted part. 

Notice: with the .net regex engine you can also use the lookbehind to test if the number of quotes before a space is even or odd, but this way isn't efficient. (same thing for a lookahead that checks remaining quotes until the end, but in addition this approach may be wrong if the quotes aren't balanced).

注意:使用.net正则表达式引擎,您还可以使用lookbehind来测试空格前的引号数是偶数还是奇数,但这种方式效率不高。 (同样的事情是先行检查剩余的报价直到结束,但另外如果报价不平衡,这种方法可能是错误的)。