Regex 在这种情况下使用什么正则表达式

Regex 在这种情况下使用什么正则表达式,regex,Regex,正确代码: "key1=val1;key2=val2;key3=val3" -- Correct as each pair is having ";" at the end except the last pair "key1=val1;key2=val2; key3=val3;" -- Invalid as last pair is having ";" at the end "key1=v

正确代码:

"key1=val1;key2=val2;key3=val3"    -- Correct as each pair is having ";" at the end except the last pair
"key1=val1;key2=val2; key3=val3;"    -- Invalid as last pair is having ";" at the end
"key1=val1;;;key2=val2;;;key3=val3"   -- Invalid as there are multiple ";" in the middle
错误代码:

"key1=val1;key2=val2;key3=val3"    -- Correct as each pair is having ";" at the end except the last pair
"key1=val1;key2=val2; key3=val3;"    -- Invalid as last pair is having ";" at the end
"key1=val1;;;key2=val2;;;key3=val3"   -- Invalid as there are multiple ";" in the middle
我从stackoverflow中的一些旧链接获得下面的正则表达式,但在上述情况下它不起作用:

^(?:\s*\w+\s*=\s*[^;]*;)+$
你可以用

^\w+\s*=\s*\w+(?:;\s*\w+\s*=\s*\w+)*$
解释

                         EXPLANATION
--------------------------------------------------------------------------------
  ^                        the beginning of the string
--------------------------------------------------------------------------------
  (?:                      group, but do not capture (1 or more times
                           (matching the most amount possible)):
--------------------------------------------------------------------------------
    \s*                      whitespace (\n, \r, \t, \f, and " ") (0
                             or more times (matching the most amount
                             possible))
--------------------------------------------------------------------------------
    \w+                      word characters (a-z, A-Z, 0-9, _) (1 or
                             more times (matching the most amount
                             possible))
--------------------------------------------------------------------------------
    \s*                      whitespace (\n, \r, \t, \f, and " ") (0
                             or more times (matching the most amount
                             possible))
--------------------------------------------------------------------------------
    =                        '='
--------------------------------------------------------------------------------
    \s*                      whitespace (\n, \r, \t, \f, and " ") (0
                             or more times (matching the most amount
                             possible))
--------------------------------------------------------------------------------
    \w+                      word characters (a-z, A-Z, 0-9, _) (1 or
                             more times (matching the most amount
                             possible))
--------------------------------------------------------------------------------
    (?:                      group, but do not capture:
--------------------------------------------------------------------------------
      ;                        ';'
--------------------------------------------------------------------------------
      (?!                      look ahead to see if there is not:
--------------------------------------------------------------------------------
        \s*                      whitespace (\n, \r, \t, \f, and " ")
                                 (0 or more times (matching the most
                                 amount possible))
--------------------------------------------------------------------------------
        $                        before an optional \n, and the end
                                 of the string
--------------------------------------------------------------------------------
      )                        end of look-ahead
--------------------------------------------------------------------------------
     |                        OR
--------------------------------------------------------------------------------
      \s*                      whitespace (\n, \r, \t, \f, and " ")
                               (0 or more times (matching the most
                               amount possible))
--------------------------------------------------------------------------------
      $                        before an optional \n, and the end of
                               the string
--------------------------------------------------------------------------------
    )                        end of grouping
--------------------------------------------------------------------------------
  )+                       end of grouping
--------------------------------------------------------------------------------
  \s*                      whitespace (\n, \r, \t, \f, and " ") (0 or
                           more times (matching the most amount
                           possible))
--------------------------------------------------------------------------------
  $                        before an optional \n, and the end of the
                           string
  • ^
    字符串的开头
  • \w+\s*=\s*\w+
    将1+字字符、
    =
    和1+字字符与可选空白字符匹配
  • (?:
    非捕获组
    • ;\s*\w+\s*=\s*\w+
      匹配
      和上面提到的相同模式
  • )*
    关闭组并重复0多次
  • $
    字符串结尾

用双倍的反斜杠

^\\w+\\s*=\\s*\\w+(?:;\\s*\\w+\\s*=\\s*\\w+)*$
你可以用

^\w+\s*=\s*\w+(?:;\s*\w+\s*=\s*\w+)*$
解释

                         EXPLANATION
--------------------------------------------------------------------------------
  ^                        the beginning of the string
--------------------------------------------------------------------------------
  (?:                      group, but do not capture (1 or more times
                           (matching the most amount possible)):
--------------------------------------------------------------------------------
    \s*                      whitespace (\n, \r, \t, \f, and " ") (0
                             or more times (matching the most amount
                             possible))
--------------------------------------------------------------------------------
    \w+                      word characters (a-z, A-Z, 0-9, _) (1 or
                             more times (matching the most amount
                             possible))
--------------------------------------------------------------------------------
    \s*                      whitespace (\n, \r, \t, \f, and " ") (0
                             or more times (matching the most amount
                             possible))
--------------------------------------------------------------------------------
    =                        '='
--------------------------------------------------------------------------------
    \s*                      whitespace (\n, \r, \t, \f, and " ") (0
                             or more times (matching the most amount
                             possible))
--------------------------------------------------------------------------------
    \w+                      word characters (a-z, A-Z, 0-9, _) (1 or
                             more times (matching the most amount
                             possible))
--------------------------------------------------------------------------------
    (?:                      group, but do not capture:
--------------------------------------------------------------------------------
      ;                        ';'
--------------------------------------------------------------------------------
      (?!                      look ahead to see if there is not:
--------------------------------------------------------------------------------
        \s*                      whitespace (\n, \r, \t, \f, and " ")
                                 (0 or more times (matching the most
                                 amount possible))
--------------------------------------------------------------------------------
        $                        before an optional \n, and the end
                                 of the string
--------------------------------------------------------------------------------
      )                        end of look-ahead
--------------------------------------------------------------------------------
     |                        OR
--------------------------------------------------------------------------------
      \s*                      whitespace (\n, \r, \t, \f, and " ")
                               (0 or more times (matching the most
                               amount possible))
--------------------------------------------------------------------------------
      $                        before an optional \n, and the end of
                               the string
--------------------------------------------------------------------------------
    )                        end of grouping
--------------------------------------------------------------------------------
  )+                       end of grouping
--------------------------------------------------------------------------------
  \s*                      whitespace (\n, \r, \t, \f, and " ") (0 or
                           more times (matching the most amount
                           possible))
--------------------------------------------------------------------------------
  $                        before an optional \n, and the end of the
                           string
  • ^
    字符串的开头
  • \w+\s*=\s*\w+
    将1+字字符、
    =
    和1+字字符与可选空白字符匹配
  • (?:
    非捕获组
    • ;\s*\w+\s*=\s*\w+
      匹配
      和上面提到的相同模式
  • )*
    关闭组并重复0多次
  • $
    字符串结尾

用双倍的反斜杠

^\\w+\\s*=\\s*\\w+(?:;\\s*\\w+\\s*=\\s*\\w+)*$
请尝试以下内容:

.*\w;\w.*\w;\w.*[^;]$
试验

说明:

  • *-->匹配任何字符
  • \w-->匹配任何单词字符
  • [^;]$-->将排除以结尾的任何行
尝试以下操作:

.*\w;\w.*\w;\w.*[^;]$
试验

说明:

  • *-->匹配任何字符
  • \w-->匹配任何单词字符
  • [^;]$-->将排除以结尾的任何行
      我发现这样的事情没有正则表达式就容易多了。对于使用JavaScript的eample:

      function isValid(string) {
        return string.split(/;/).map(e => e.split(/=/)).every(e => e.length === 2);
      }
      

      我发现这样的事情没有正则表达式就容易多了。对于使用JavaScript的eample:

      function isValid(string) {
        return string.split(/;/).map(e => e.split(/=/)).every(e => e.length === 2);
      }
      
      还有一个较短的:

      ^(?:\s*\w+\s*=\s*\w+(?:;(?!\s*$)|\s*$))+\s*$
      

      解释

                               EXPLANATION
      --------------------------------------------------------------------------------
        ^                        the beginning of the string
      --------------------------------------------------------------------------------
        (?:                      group, but do not capture (1 or more times
                                 (matching the most amount possible)):
      --------------------------------------------------------------------------------
          \s*                      whitespace (\n, \r, \t, \f, and " ") (0
                                   or more times (matching the most amount
                                   possible))
      --------------------------------------------------------------------------------
          \w+                      word characters (a-z, A-Z, 0-9, _) (1 or
                                   more times (matching the most amount
                                   possible))
      --------------------------------------------------------------------------------
          \s*                      whitespace (\n, \r, \t, \f, and " ") (0
                                   or more times (matching the most amount
                                   possible))
      --------------------------------------------------------------------------------
          =                        '='
      --------------------------------------------------------------------------------
          \s*                      whitespace (\n, \r, \t, \f, and " ") (0
                                   or more times (matching the most amount
                                   possible))
      --------------------------------------------------------------------------------
          \w+                      word characters (a-z, A-Z, 0-9, _) (1 or
                                   more times (matching the most amount
                                   possible))
      --------------------------------------------------------------------------------
          (?:                      group, but do not capture:
      --------------------------------------------------------------------------------
            ;                        ';'
      --------------------------------------------------------------------------------
            (?!                      look ahead to see if there is not:
      --------------------------------------------------------------------------------
              \s*                      whitespace (\n, \r, \t, \f, and " ")
                                       (0 or more times (matching the most
                                       amount possible))
      --------------------------------------------------------------------------------
              $                        before an optional \n, and the end
                                       of the string
      --------------------------------------------------------------------------------
            )                        end of look-ahead
      --------------------------------------------------------------------------------
           |                        OR
      --------------------------------------------------------------------------------
            \s*                      whitespace (\n, \r, \t, \f, and " ")
                                     (0 or more times (matching the most
                                     amount possible))
      --------------------------------------------------------------------------------
            $                        before an optional \n, and the end of
                                     the string
      --------------------------------------------------------------------------------
          )                        end of grouping
      --------------------------------------------------------------------------------
        )+                       end of grouping
      --------------------------------------------------------------------------------
        \s*                      whitespace (\n, \r, \t, \f, and " ") (0 or
                                 more times (matching the most amount
                                 possible))
      --------------------------------------------------------------------------------
        $                        before an optional \n, and the end of the
                                 string
      
      还有一个较短的:

      ^(?:\s*\w+\s*=\s*\w+(?:;(?!\s*$)|\s*$))+\s*$
      

      解释

                               EXPLANATION
      --------------------------------------------------------------------------------
        ^                        the beginning of the string
      --------------------------------------------------------------------------------
        (?:                      group, but do not capture (1 or more times
                                 (matching the most amount possible)):
      --------------------------------------------------------------------------------
          \s*                      whitespace (\n, \r, \t, \f, and " ") (0
                                   or more times (matching the most amount
                                   possible))
      --------------------------------------------------------------------------------
          \w+                      word characters (a-z, A-Z, 0-9, _) (1 or
                                   more times (matching the most amount
                                   possible))
      --------------------------------------------------------------------------------
          \s*                      whitespace (\n, \r, \t, \f, and " ") (0
                                   or more times (matching the most amount
                                   possible))
      --------------------------------------------------------------------------------
          =                        '='
      --------------------------------------------------------------------------------
          \s*                      whitespace (\n, \r, \t, \f, and " ") (0
                                   or more times (matching the most amount
                                   possible))
      --------------------------------------------------------------------------------
          \w+                      word characters (a-z, A-Z, 0-9, _) (1 or
                                   more times (matching the most amount
                                   possible))
      --------------------------------------------------------------------------------
          (?:                      group, but do not capture:
      --------------------------------------------------------------------------------
            ;                        ';'
      --------------------------------------------------------------------------------
            (?!                      look ahead to see if there is not:
      --------------------------------------------------------------------------------
              \s*                      whitespace (\n, \r, \t, \f, and " ")
                                       (0 or more times (matching the most
                                       amount possible))
      --------------------------------------------------------------------------------
              $                        before an optional \n, and the end
                                       of the string
      --------------------------------------------------------------------------------
            )                        end of look-ahead
      --------------------------------------------------------------------------------
           |                        OR
      --------------------------------------------------------------------------------
            \s*                      whitespace (\n, \r, \t, \f, and " ")
                                     (0 or more times (matching the most
                                     amount possible))
      --------------------------------------------------------------------------------
            $                        before an optional \n, and the end of
                                     the string
      --------------------------------------------------------------------------------
          )                        end of grouping
      --------------------------------------------------------------------------------
        )+                       end of grouping
      --------------------------------------------------------------------------------
        \s*                      whitespace (\n, \r, \t, \f, and " ") (0 or
                                 more times (matching the most amount
                                 possible))
      --------------------------------------------------------------------------------
        $                        before an optional \n, and the end of the
                                 string
      

      我有东西。任何人都可以验证(^(?:\\s*\\w+\\s*=\\s*[^;]*;)+).[^;]$键或值是否包含
      =
      =
      如果是,它们是如何转义的?
      [\w]
      -->
      \w
      。另外,这里不需要点星汤。我用。*来选择行中的其余字符。如果没有这些,我怀疑它是否有效。另外,这里不需要点星汤。我用。*来选择行中的其余字符。如果没有这一点,我怀疑它是否有效。。这不适用于以下键1=val1;key2=val2 key3=val3不应拾取上面的内容,因为缺少一个;在键2之后=val2@user10278830它与那不匹配。。这不适用于以下键1=val1;key2=val2 key3=val3不应拾取上面的内容,因为缺少一个;在键2之后=val2@user10278830它和那个不匹配