Subset regular expression substitution

David R. Waddell dave.waddell at wcom.com
Thu Feb 21 12:46:09 CST 2002


Hi Nelson,
There is another way that is probably more efficient than
regular expressions for this. It also may be more understandable
when you look at the code:

while(<>){
	substr($_,0,10) =~ tr/()/ /
}
continue{
	print STDOUT
}

It only performs the tr on the first 10
characters.

all the best,
david
At 05:00 PM 2/13/02 -0700, Nelson Ingersoll wrote:
>Greetings Fellow Mongers,
>
>    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!
>
>- Nelson ...
>
>=====================================================================
>Nelson E. Ingersoll             <> Sr. Principal Software Grunt
>---------------------------------------------------------------------
>ATMEL Corporation               <> Desk:     719-540-1263
>Mail Stop 10240                 <> FAX:      719-540-6998
>1150 E. Cheyenne Mtn Blvd.      <> Pager:    719-921-7917
>Colorado Springs, CO  80906     <> INTERNET: ningersoll at cso.atmel.com
>United States of America        <>           ningersoll at codenet.net
>================== No good deed goes unpunished! ====================
>
>



More information about the Pikes-peak-pm mailing list