[thelist] regex: replacing bracket characters

David Dorward evolt at david.us-lot.org
Wed Dec 10 08:47:44 CST 2003


On 10 Dec 2003, at 14:05, Tom Dell'Aringa wrote:
>
> I want to replace the ( and ) characters in a string (for example,
> strip them out of the area code in a phone number). I can't seem to
> figure out the pattern for these characters.
>
> I've tried both:
>
> objRegExp = /()/g;
> objRegExp = /\(\)/g;
>

1. You aren't saying you want to make a substitution (so add 's')

s/\(\)/g

2. You aren't saying what you want to substitute them with (so add /)

s/\(\)//g

3. You are trying to match "()" rather then "(" or ")" (so wrap them in 
brackets and stick a | between them)

s/(\(|\))//g

So:

$ perl
$foo = "+44 (7777) 123456";
$foo =~ s/(\(|\))//g;
print $foo . "\n";


Outputs:
+44 7777 123456

--
David Dorward
http://dorward.me.uk/



More information about the thelist mailing list