开发者

Regex Exception in rows

开发者 https://www.devze.com 2023-02-28 20:37 出处:网络
Say i\'ve got an regex: \"|<pizza><o开发者_StackOverflow社区nion>(.*)</onion><tomato>(.*)</tomato></pizza>|\"

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消