Regex将子句的键、运算符和值分组
从如下图所示的子句中获取一组键、运算符和值的最佳正则表达式是什么 到目前为止,我所做的并不准确,只能得到第一组:Regex将子句的键、运算符和值分组,regex,regex-group,Regex,Regex Group,从如下图所示的子句中获取一组键、运算符和值的最佳正则表达式是什么 到目前为止,我所做的并不准确,只能得到第一组:(^.*?(=|!=)+([^.]*)) 使用 (\w+(?:\.\w+)*)\s*(!=|=)\s*(\w+) 看 解释 -------------------------------------------------------------------------------- ( group and capture to \1
(^.*?(=|!=)+([^.]*))
使用
(\w+(?:\.\w+)*)\s*(!=|=)\s*(\w+)
看
解释
--------------------------------------------------------------------------------
( group and capture to \1:
--------------------------------------------------------------------------------
\w+ word characters (a-z, A-Z, 0-9, _) (1 or
more times (matching the most amount
possible))
--------------------------------------------------------------------------------
(?: group, but do not capture (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))
--------------------------------------------------------------------------------
)* end of grouping
--------------------------------------------------------------------------------
) end of \1
--------------------------------------------------------------------------------
\s* whitespace (\n, \r, \t, \f, and " ") (0 or
more times (matching the most amount
possible))
--------------------------------------------------------------------------------
( group and capture to \2:
--------------------------------------------------------------------------------
!= '!='
--------------------------------------------------------------------------------
| OR
--------------------------------------------------------------------------------
= '='
--------------------------------------------------------------------------------
) end of \2
--------------------------------------------------------------------------------
\s* whitespace (\n, \r, \t, \f, and " ") (0 or
more times (matching the most amount
possible))
--------------------------------------------------------------------------------
( group and capture to \3:
--------------------------------------------------------------------------------
\w+ word characters (a-z, A-Z, 0-9, _) (1 or
more times (matching the most amount
possible))
--------------------------------------------------------------------------------
) end of \3
显然你不知道从哪里开始你的正则表达式。有关正则表达式的详细信息,请查看和[Learning Regular Expressions}()。不太清楚正则表达式应该遵循哪些规则。请尝试
(?:^ |\b(和|或)\s+(\w[\w.]*)\s+(!?=)\s+(\w+)
。请参阅。