如何使用组、否定、环视在正则表达式中的模式之后匹配模式
How to match a pattern after a pattern in regex, using groups, negations, lookarounds
我有一个模式" -[^ ]* "
,这里的引号是为了显示空格。我想匹配它之后的所有内容,直到再次遇到这种模式
像这样
(?<= -[^ ]* )[^( -[^ ]* )]*
或紧凑形式
p = ( -[^ ]* )
(?<=p)[^p]*
问题是我不能在 (?<= -[^ ]* )
语句中使用 *
并且我不能在 [^( -[^ ]* )]
中使用 ()
。在这两种情况下,这些字符都按字面意思处理
[^( -[^ ]* )]*
是否定字符class,它匹配不同于圆括号、空格、连字符、脱字符等的字符。
使用
-[^ ]* ((?:(?! -[^ ]* ).)*)
说明
--------------------------------------------------------------------------------
- '-'
--------------------------------------------------------------------------------
[^ ]* any character except: ' ' (0 or more times
(matching the most amount possible))
--------------------------------------------------------------------------------
' '
--------------------------------------------------------------------------------
( group and capture to :
--------------------------------------------------------------------------------
(?: group, but do not capture (0 or more
times (matching the most amount
possible)):
--------------------------------------------------------------------------------
(?! look ahead to see if there is not:
--------------------------------------------------------------------------------
- ' -'
--------------------------------------------------------------------------------
[^ ]* any character except: ' ' (0 or more
times (matching the most amount
possible))
--------------------------------------------------------------------------------
' '
--------------------------------------------------------------------------------
) end of look-ahead
--------------------------------------------------------------------------------
. any character except \n
--------------------------------------------------------------------------------
)* end of grouping
--------------------------------------------------------------------------------
) end of
我有一个模式" -[^ ]* "
,这里的引号是为了显示空格。我想匹配它之后的所有内容,直到再次遇到这种模式
像这样
(?<= -[^ ]* )[^( -[^ ]* )]*
或紧凑形式
p = ( -[^ ]* )
(?<=p)[^p]*
问题是我不能在 (?<= -[^ ]* )
语句中使用 *
并且我不能在 [^( -[^ ]* )]
中使用 ()
。在这两种情况下,这些字符都按字面意思处理
[^( -[^ ]* )]*
是否定字符class,它匹配不同于圆括号、空格、连字符、脱字符等的字符。
使用
-[^ ]* ((?:(?! -[^ ]* ).)*)
说明
--------------------------------------------------------------------------------
- '-'
--------------------------------------------------------------------------------
[^ ]* any character except: ' ' (0 or more times
(matching the most amount possible))
--------------------------------------------------------------------------------
' '
--------------------------------------------------------------------------------
( group and capture to :
--------------------------------------------------------------------------------
(?: group, but do not capture (0 or more
times (matching the most amount
possible)):
--------------------------------------------------------------------------------
(?! look ahead to see if there is not:
--------------------------------------------------------------------------------
- ' -'
--------------------------------------------------------------------------------
[^ ]* any character except: ' ' (0 or more
times (matching the most amount
possible))
--------------------------------------------------------------------------------
' '
--------------------------------------------------------------------------------
) end of look-ahead
--------------------------------------------------------------------------------
. any character except \n
--------------------------------------------------------------------------------
)* end of grouping
--------------------------------------------------------------------------------
) end of