开发者

regex question: independent position of words

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜