开发者

regex question: independent position of words

开发者 https://www.devze.com 2022-12-25 22:07 出处:网络
is it possible to define a regex pattern which checks eg. for 3 terms independent to their position in the ma开发者_Go百科in string?

is it possible to define a regex pattern which checks eg. for 3 terms independent to their position in the ma开发者_Go百科in string?

eg. my string is something like "click here to unsubscribe: http://www.url.com"

the pattern should also work with "http:// unsubscribe click"

thx


You can use positive lookaheads. For example,

(?=.*click)(?=.*unsubscribe).*http

is a regex that will look ahead from the current position (without moving ahead) for click, then for unsubscribe, then search normally for http.


It's possible, but results in very complicated regexs, e.g.:

/(click.*unsubscribe|unsubscribe.*click)/

Basically, you would need to have a different regex section for each order. Not ideal. Better to just use multiple regexes, one for each term.


Yes, using conditionals.

http://www.regular-expressions.info/conditional.html

0

精彩评论

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