Subset regular expression substitution

Nelson Ingersoll ningersoll at cso.atmel.com
Thu Feb 14 11:32:46 CST 2002


Keary!

     Beautimous!  Works like a charm.  However, I am not familiar with the 
syntax

         s|^(.{10})| ... |e;

Looks typically cryptic and probably called something odd.  Can you point 
me to where I might learn more about it?  I have most of the Perl books and 
this is the first time I've ever seen it.   I feel so naive!

    Thank you!

- Nelson ...
   Ignorance knows what stupidity doesn't.

At 2/13/2002 05:28 PM, Keary Suska wrote:
>on 2/13/02 5:00 PM, ningersoll at cso.atmel.com purportedly said:
>
> > I have a string which may, or may not, have parenthesis somewhere in
> > the first 10 characters.  The string is always longer than 10
> > characters.  I want to substitute space for either "(" or ")" or
> > both.   Because that part of the string which beyond the tenth character is
> > descriptive text, I dare not change any parenthesis after the first 10
> > characters.  I understand the regex {$x =~ s/\(|\)/ /g;} matches any and
> > all occurrences of either the '(' or ')' characters and replaces any '(' or
> > ')' with a space.
> >
> > The issue is, can I force the substitution, "s/\(|\)/ /g", to only work
> > over the first 10 characters of $x rather than all characters?  Is this
> > possible in a regex?   If not, I can always sub-string wing it.
> >
> > Your thoughts will be appreciated!
>
>TIMTOWTDI, of course. If brevity is what you want, you could use:
>
>     $text =~ s|^(.{10})|do{ $str = $1; $str =~ s/[()]/ /g; $str }|e;
>
>Anything dissimilar would require multiple steps.
>
>Keary Suska
>Esoteritech, Inc.
>"Leveraging Open Source for a better Internet"




More information about the Pikes-peak-pm mailing list