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