Is it possible to use a regex to create a pattern that matches fragments that do NOT contain a certain string?
This magic regex would take this input and examine whats between the parenthesis:
(foo bar) (barfoo) (zab) (foozab)
and only return zab开发者_运维知识库
because it doesn't contain foo
between the parenthesis.
Is this possible, or should I just capture everything between parenthesis and use a langauge function to exclude them?
Depending on the engine, you can use a lookahead assertion.
\(((?:(?!foo)[^)])+)\)
That regex will match a parenthesized string where the characters inside the string do not ever match the sub-expression "foo" (which in this case is just a string).
Here it is in expanded form:
\( # match the opening (
( # capture the text inside the parens
(?: # we need another group, but don't capture it
(?!foo) # fail if the sub-expression "foo" matches at this point
[^)] # match a non-paren character
)+ # repeat that group
) # end the capture
\) # end the parens
精彩评论