tags:

views:

71

answers:

1

I am trying to use a regex to find text inside a string. For example, have this string:

one,two,three,four

If I want to see if it has one OR two, I can use "one|two". How do I create a regex for determining whether the string has one AND two?

+7  A: 
^(?=.*\bone\b)(?=.*\btwo\b)

= two lookahead assertions that match if both "one" and "two" are present in the string.

Tim Pietzcker
Nice, wouldn't of thought to use lookaheads for that... I would add a `\b` around `one` and `two` to ensure that `onetwo` isn't matched...
gnarf
+1, it would also be useful to add `^` in the beginning to avoid extra work in case of no-match
Antony Hatchkins
excellent idea, thanks!
Tim Pietzcker