首页 > 解决方案 > 正则表达式找到一行有错误的引号

问题描述

嗨,我需要帮助才能找到有错误引号的行。

1 "many word " "many word"  "many word "
2 "many word "  " <-Error quotation
3 "many word " "many word " " many word  " " many word "    " <-Error quotation
4 """ but this quotation not match
5 "\"" this is not match
6 " aasd \" "  " <-Error quotation \"

only line 2 3 6 have an error quotation  

i need to find a line have an error quotation by regex

https://regex101.com/r/bWBV7A/9

多谢

标签: regexquotation-marks

解决方案


利用

^[^"\n]*(?:"""|"[^\\\n"]*(?:\\.[^\\\n"]*)*"[^"\n]*)*+"

证明

解释

--------------------------------------------------------------------------------
  ^                        the beginning of the string
--------------------------------------------------------------------------------
  [^"\n]*                  any character except: '"', '\n' (newline)
                           (0 or more times (matching the most amount
                           possible))
--------------------------------------------------------------------------------
  (?:                      group, but do not capture (0 or more times
                           (matching the most amount possible)):
--------------------------------------------------------------------------------
    """                      '"""'
--------------------------------------------------------------------------------
   |                        OR
--------------------------------------------------------------------------------
    "                        '"'
--------------------------------------------------------------------------------
    [^\\\n"]*                any character except: '\\', '\n'
                             (newline), '"' (0 or more times
                             (matching the most amount possible))
--------------------------------------------------------------------------------
    (?:                      group, but do not capture (0 or more
                             times (matching the most amount
                             possible)):
--------------------------------------------------------------------------------
      \\                       '\'
--------------------------------------------------------------------------------
      .                        any character except \n
--------------------------------------------------------------------------------
      [^\\\n"]*                any character except: '\\', '\n'
                               (newline), '"' (0 or more times
                               (matching the most amount possible))
--------------------------------------------------------------------------------
    )*                       end of grouping
--------------------------------------------------------------------------------
    "                        '"'
--------------------------------------------------------------------------------
    [^"\n]*                  any character except: '"', '\n'
                             (newline) (0 or more times (matching the
                             most amount possible))
--------------------------------------------------------------------------------
  )*+                       end of grouping (possessive match, no backtracking)

推荐阅读