Subset regular expression substitution
Keary Suska
hierophant at pcisys.net
Wed Feb 13 18:28:21 CST 2002
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