Forum Moderators: coopster & phranque

Message Too Old, No Replies

improve on this Perl pattern match?

         

bryndyment

5:35 pm on Mar 30, 2003 (gmt 0)

10+ Year Member



$_ = '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.

jdMorgan

5:44 pm on Mar 30, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Not sure what the ultimate goal is, but you could unconditionally add an underscore to both the beginning and end of the string, and then test only for _($foo)_

Jim

bryndyment

5:51 pm on Mar 30, 2003 (gmt 0)

10+ Year Member



That would work perfectly. Just curious, though, about a solution that doesn't involve changing that string...

ShawnR

10:45 pm on Mar 30, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



How about

/(^¦_)($foo)(_¦$)/

(It will also match if $foo is the only thing in your string.)

bryndyment

11:26 pm on Mar 30, 2003 (gmt 0)

10+ Year Member



Perfect. And having it also match when $foo is the only value in the string is important to me (now that's it's been pointed out...).