tags:

views:

66

answers:

3

hi all,

is it possible to define a regex pattern which checks eg. for 3 terms independent to their position in the main 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

+2  A: 

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.

tloflin
Or use "index" or equivalents three times.
Kinopiko
+2  A: 

Yes, using conditionals.

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

jpabluz
Can you provide more detail? I don't see how conditionals will help.
Alan Moore
+5  A: 

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.

me_and
That only works if both "click" and "unsubscribe" come *after* "http", which doesn't jibe with the OP's first example.
Alan Moore
@Alan Moore: True. Corrected.
me_and