in reply to Re^5: AND and OR on Regular Expressions
in thread AND and OR on Regular Expressions
As for the leading ^ - it only has a speed influence, and only if there's no match. But it can be a significant speed difference if the string is long, so it's better not to omit it.$_ = "foo bar foo"; $pat1 = "(foo)"; $pat2 = "(bar)"; /$pat1/ && /$pat2/; # Sets $1 (twice). /^(?=.*?$pat1)(?=.*?$pat2)/; # Sets $1 and $2. $pat1 = "(foo)"; $pat2 = "(baz)"; /$pat1/ && /$pat2/; # Sets $1. /^(?=.*?$pat1)(?=.*?$pat2)/; # Doesn't set $1 or $2. $pat1 = "(foo)"; $pat2 = "( )\\g{1}"; /$pat1/ && /$pat2/; # No match. /^(?=.*?$pat1)(?=.*?$pat2)/; # Match!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^7: AND and OR on Regular Expressions
by ikegami (Patriarch) on Aug 26, 2009 at 00:50 UTC | |
by JavaFan (Canon) on Aug 26, 2009 at 07:36 UTC | |
by ikegami (Patriarch) on Aug 26, 2009 at 14:02 UTC | |
by JavaFan (Canon) on Aug 26, 2009 at 14:06 UTC |