[oclug]Perl Matching Q
Ross Jordan
rjordan at student.math.uwaterloo.ca
Wed Nov 27 14:38:19 EST 2002
>
> If I have an array of strings, each of which can have the form:
>
> "name" or
> "namelonger" or
> "name/r" or other such combinations
>
> (where '/r' above is just a '/r').
>
> What sort of regex recipe would I need to match just 'name' (and not
> 'namelonger') _and_ retain any possible non-word chars (the '/' if
> present)? If I specify 's/$name\b/$newname/g', I'd lose the '/'
> character.
In sed, one might do something like:
s/name\([^a-zA-Z]*\).*/\1newname)/
The above would move the non-word chars to the beginning.
The non word chars are remembered and later recalled using \1
Not sure if Perl has similar.
-Ross
More information about the OCLUG
mailing list