简体   繁体   English

正则表达式只能匹配两种类型的带引号的字符串之一

[英]regex should match only one of two types of quoted strings

I need a regex that match a string that is surrounded by double quotes. 我需要一个正则表达式来匹配用双引号引起来的字符串。 It should not match a string surrounded by double quotes if this pattern is surrounded by single quotes: 如果此模式用单引号引起来,则它不应与用双引号引起来的字符串匹配:

"string"
" 'xyz' "
"  `"    "
"  `" `"   "
"  `" `" `"  "
'  ' "should match" '  '
'   "should not match"   '

Now I have ( https://regex101.com/r/z5PayV/1 ) 现在我有( https://regex101.com/r/z5PayV/1

(?:"(([^"]*`")*[^"]*|[^"]*)") 

that matches all lines. 匹配所有行。 But the last line should not be matched. 但是最后一行不应该匹配。 Any solution? 有什么办法吗?

You have to go past single quotes to exclude them from the match 您必须经过单引号才能将其从匹配项中排除

update 更新

For C# it has to be done like this. 对于C#,必须像这样完成。
Just uses a simple CaptureCollection to get all 只需使用一个简单的CaptureCollection即可获取全部
the quoted matches. 引用的匹配项。

(?:'[^']*'|(?:"(([^"]*`")*[^"]*|[^"]*)")|[\S\s])+

Expanded 扩展

 (?:
      ' [^']* '

   |  
      (?:
           "
           (                             # (1 start)
                ( [^"]* `" )*                 # (2)
                [^"]* 
             |  [^"]* 
           )                             # (1 end)
           "
      )
   |  
      [\S\s] 
 )+

C# code C#代码

var str =
"The two sentences are 'He said \"Hello there\"' and \"She said 'goodbye' and 'another sentence'\"\n" +
"\"  `\"    \"\n" +
"\"  `\"    \"\n" +
"\"  `\" `\"   \"\n" +
"\"  `\" `\" `\"  \"\n" +
"'   \"   \"   '\n" +
"\"string\"\n" +
"\" 'xyz' \"\n" +
"\"  `\"    \"\n" +
"\"  `\" `\"   \"\n" +
"\"  `\" `\" `\"  \"\n" +
"'  ' \"should match\" '  '\n" +
"'   \"should not match\"   '\n";

var rx = new Regex( "(?:'[^']*'|(?:\"(([^\"]*`\")*[^\"]*|[^\"]*)\")|[\\S\\s])+" );

Match M = rx.Match( str );
if (M.Success)
{
    CaptureCollection cc = M.Groups[1].Captures;
    for (int i = 0; i < cc.Count; i++)
        Console.WriteLine("{0}", cc[i].Value);
}

Output 产量

She said 'goodbye' and 'another sentence'
  `"
  `"
  `" `"
  `" `" `"
string
 'xyz'
  `"
  `" `"
  `" `" `"
should match

Excuse this, it is the way it's done in PCRE engine 抱歉,这是在PCRE引擎中完成的方式

'[^']*'(*SKIP)(*FAIL)|(?:"(([^"]*`")*[^"]*|[^"]*)")`

https://regex101.com/r/gMiVDU/1 https://regex101.com/r/gMiVDU/1

   ' [^']* '
   (*SKIP) (*FAIL) 
|  
   (?:
        "
        (                             # (1 start)
             ( [^"]* `" )*                 # (2)
             [^"]* 
          |  [^"]* 
        )                             # (1 end)
        "
   )

___________________________- ___________________________-

The answer looks quite complex, how is this one: 答案看起来很复杂,这是怎么回事:

^"(\\d+|\\D+)"$ ^ “(\\ d + | \\ d +)” $

is it too simple? 太简单了吗?

The idea here is to check the string starts and ends with the double quote ("), anything within the double quote including the single quote is allowed. 这里的想法是检查字符串以双引号(“)开始和结束,双引号内的任何内容(包括单引号)都是允许的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM