PDA

View Full Version : improve on this pattern match?


bryndyment
03-30-2003, 06:32 PM
$_ = '1_2_3_4_5';
$foo = '3';

/^($foo)_|_($foo)_|_($foo)$/

I want to match $foo if I find it bounded by underscores, or at the beginning of the string followed by an underscore, or at the end of the string preceded by an underscore.

I'm wondering if there's a way to do this without the alternation (|) characters.

chrisvmarle
03-30-2003, 09:38 PM
I'm not sure and I haven't test it either, but you could try this:

$_ = '1_2_3_4_5';
$foo = '3';

/(^|_)($foo)(_|$)/

Mzzl, Chris

bryndyment
03-30-2003, 09:45 PM
Elegant and functional... thanks!

chrisvmarle
03-30-2003, 10:55 PM
IT WORKS?! :eek:
Nice :o