SPUG: Re: regex problems...
charles.e.derykus at boeing.com
charles.e.derykus at boeing.com
Fri Oct 1 21:57:23 CDT 1999
> So I figured out two things:
> 1. is that I can use parentheses on the left side of the expression but not
> the right (shown above)
> 2. is that I've found (\w\w) doesn't allow the expression to work -- instead
> I assigned a variable to equal '\w\w' and used that in it's place
> 3. is that my variables need to have special characters escaped on the left,
> but not escaped on the right (not shown above, the code above I think should
> work, but don't understand why it doesn't).
> Is it really necessary to change my $after variables to escape the '.' and
> '?' ? Or is there a cleaner way of doing this? The main reason for my
> asking is not only do I need to add images, but I also need to take them
> away, so I end up with 4 sets of variables, two escaped, and two unescaped.
> Very messy!
Right. Variables in the regex replacement don't need to
to be escaped; in the regex pattern they do.
for example:
my $foobar = "... (*abc?) ...";
# Notice \Q operator to autoescape regex metacharacter's
# See: perldoc perlop and look for \Q
my $foo = "\Q(*abc?)";
# the replacement part $bar is full of metachar's but
# doesn't need to be escaped.
my $bar = "(*foobar?)";
$foobar =~ s/$foo/$bar/igs;
Rgds,
--
Charles DeRykus
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
POST TO: spug-list at pm.org PROBLEMS: owner-spug-list at pm.org
Seattle Perl Users Group (SPUG) Home Page: http://www.halcyon.com/spug/
SUBSCRIBE/UNSUBSCRIBE: Replace ACTION below by subscribe or unsubscribe
Email to majordomo at pm.org: ACTION spug-list your_address
More information about the spug-list
mailing list