【问题标题】:Regex: Require that quotes are escaped in a string正则表达式:要求引号在字符串中转义
【发布时间】:2012-02-03 12:28:39
【问题描述】:

感谢收看,

我很难为这个正则表达式问题找到正确的搜索词。我需要确保引号已经在字符串中转义,否则匹配会失败。 (此类问题的大多数搜索结果只是说明您需要转义引号或如何转义引号的页面。)

有效:

This is valid
This \"is Valid
This is al\"so Valid\"

无效:

This i"s invalid
This i"s inv"alid

到目前为止,我唯一能找到的是

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

这似乎与下面的第一部分相匹配,但转义引号后没有任何内容

This is a \"test

同样,这应该失败:

This is a \"test of " the emergency broadcast system

感谢您的帮助,我希望这是可能的。

【问题讨论】:

  • +1,有趣的问题,REs 实际上似乎是正确的工具。
  • @JosephSilber 我实际上没有使用语言,这个正则表达式将进入 CMS 中使用的正则表达式字段,需要用户输入来匹配它:)
  • 没有not using a language 这样的东西。 CMS 使用什么语言?
  • 需要注意的陷阱之一是a\\"ba\\\\"b 无效,但a\\\"ba\\\\\"b 有效。这是因为前两个示例中引号前面的偶数个反斜杠被绑定为转义,即使引号前面有一个反斜杠,引号也不会转义,而后两个示例中引号前面的奇数个反斜杠是可以的因为偶数个反斜杠引用反斜杠,奇数反斜杠转义引号。
  • 非常感谢大家的帮助!

标签: regex


【解决方案1】:

在 C# 中,这似乎可以按您的意愿工作:

string pattern = "^([^\"\\\\]*(\\\\.)?)*$";

去掉转义后留下:

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

大致翻译为:字符串开头,(多字符不包括引号或反斜杠,可选反斜杠任何字符)-重复,字符串结尾

字符串的开头和结尾标记强制匹配整个文本。

【讨论】:

  • 这对我来说效果很好,除了一种情况:这是\“有效\我相信虽然我不太可能在最后得到一个斜杠的输入并且可以让失败了。
  • 是的,它被设计为在末尾有一个反斜杠的情况下失败。原因是如果字符串支持使用反斜杠的转义字符,则单个反斜杠永远不会有效。
【解决方案2】:

不知道您使用的语言,但我会这样做:

制作一个正则表达式,匹配没有反斜杠的引号,这将失败

This is a \"test

成功了

This is a \"test of " the emergency broadcast system

例如这个:

.*(?<!\\)".*

然后将使用否定表达式来处理结果。 希望对你有帮助

我在java中的测试看起来像

    String pat = ".*(?<!\\\\)\".*";
    String s = "This is a \\\"test";
    System.out.println(!s.matches(pat));
    s = "This is a \\\"test of \" the emergency broadcast system";
    System.out.println(!s.matches(pat));

【讨论】:

    【解决方案3】:

    您想使用消极的后视。

    (?<!\\)"
    

    此正则表达式将匹配所有前面没有单斜杠的引号。

    如果您对示例字符串运行此正则表达式并找到 1 个或多个匹配项,则该字符串无效。

    【讨论】:

      【解决方案4】:

      除了反斜杠和引号或反斜杠和下一个字符之外,您需要获取所有内容。

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

      这样,这将失败:

      ab\\"c
      

      这会成功:

      ab\\\"c
      

      这会成功:

      ab\"c
      

      【讨论】:

        【解决方案5】:

        您正在寻找的正则表达式是:

        /^(?:[^"]*(?:(?<=\\\)"|))*$/
        

        解释: [^"]* 将匹配输入,直到找到第一个 " 或到达输入结尾。如果找到",则确保在(?&lt;=\\\)"lookbehind 中always 前面有/。上述场景递归重复直到输入结束。

        测试:考虑以下 PHP 代码进行测试:

        $arr=array('This is valid',
        'This \"is Valid',
        'This is al\"so Valid\"',
        'This i"s invalid',
        'This i"s inv"alid',
        'This is a \"test',
        'This is a \"test of " the emergency broadcast system - invalid');
        foreach ($arr as $a) {
           echo "$a => ";
           if (preg_match('/^(?:[^"]*(?:(?<=\\\)"|))*$/', $a, $m))
              echo "matched [$m[0]]\n";
           else
              echo "didn't match\n";
        }
        

        输出:

        This is valid => matched [This is valid]
        This \"is Valid => matched [This \"is Valid]
        This is al\"so Valid\" => matched [This is al\"so Valid\"]
        This i"s invalid => didn't match
        This i"s inv"alid => didn't match
        This is a \"test => matched [This is a \"test]
        This is a \"test of " the emergency broadcast system - invalid => didn't match
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2010-09-19
          • 2011-09-25
          • 2010-09-21
          相关资源
          最近更新 更多