Regex Exception in rows
Say i've got an regex:
"|<pizza><o开发者_StackOverflow社区nion>(.*)</onion><tomato>(.*)</tomato></pizza>|"
This one matches a few rows and returns it (php: preg_match_all). Now I want to add an exception to (.*) in onion: onion may not have the value "default". How can I exclude this one?
Try:
~<pizza><onion>((?:(?!</onion>|default).)*)</onion><tomato>((?:(?!</tomato>).)*)</tomato></pizza>~s
Or the equivalent, but with the x
flag enabled:
~
<pizza>
<onion>
(
(?:(?!</onion>|default).)* # anything not containing `</onion>` and `default`
)
</onion>
<tomato>
(
(?:(?!</tomato>).)* # anything not containing `</tomato>`
)
</tomato>
</pizza>
~sx # enable DOT-ALL (s), and COMMENTS (x)
where ~
is the delimiter.
If your source has comments, <!-- possible tags -->
, or can have an arbitrary number of nested tags, I highly recommend using some sort of (X)HTML-ish parser instead.
精彩评论