in reply to AND and OR on Regular Expressions
Thanks to everyone.
It seems that the following code does what I want:
#!perl -w my $test = ( "olive&(popeye|bluto)", "(tom&jerry)|(sylvester&tweety)", "moe&(shemp|curly|joe)&larry", "tom&jerry|sylvester&tweety", # valid "moe ( shemp | curly | joe ) larry", # also valid "moe(&shemp|curly|joe)&larry", # invalid: "(&" instead of "&(" "moe ( shemp | curly | joe", # invalid: missing ")" raises an error )[(shift) - 1]; my $expr = $test; $expr =~ s/(\w+)/"(?=.*\\b($1)\\b)"/ge; $expr =~ s/[\&\s]//g; $expr = "^($expr)"; print "$test\n$expr\n----\n"; while (<DATA>) { print $_ if /$expr/; } __DATA__ tom,jerry jerry,tom jerry,tomas sylvester,tweety tweeter,sylvester tom,sylvester popeye,olive olive,brutus moe,larry shemp,curly,joe larry,moe larry,curly,moe
>re.pl 1 olive&(popeye|bluto) ^((?=.*\b(olive)\b)((?=.*\b(popeye)\b)|(?=.*\b(bluto)\b))) ---- popeye,olive >re.pl 2 (tom&jerry)|(sylvester&tweety) ^(((?=.*\b(tom)\b)(?=.*\b(jerry)\b))|((?=.*\b(sylvester)\b)(?=.*\b(twe +ety)\b))) ---- tom,jerry jerry,tom sylvester,tweety >re.pl 3 moe&(shemp|curly|joe)&larry ^((?=.*\b(moe)\b)((?=.*\b(shemp)\b)|(?=.*\b(curly)\b)|(?=.*\b(joe)\b)) +(?=.*\b(larry)\b)) ---- larry,curly,moe
I know this will raise an error if the expression is invalid, but I'm sure it could be checked first with something "simple" like the following:
die "Invalid expression <$test>\n" if $test =~ /[^a-z\s\&\|\(\)]|^\s*[\&\|]|[\&\|]\s*$|[\&\|]\s*[\&\|]| +[\&\|]\s*\)|\(\s*[\|\&]/;
and I'm sure I can find something more to validate that parenthesis are well paired.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: AND and OR on Regular Expressions
by vitoco (Hermit) on Aug 25, 2009 at 21:18 UTC | |
by grizzley (Chaplain) on Aug 26, 2009 at 07:19 UTC | |
by vitoco (Hermit) on Aug 26, 2009 at 14:51 UTC |
In Section
Seekers of Perl Wisdom