[Raleigh-talk] Please assist w/regex

James Olin Oden james.oden at gmail.com
Tue Mar 20 11:07:58 PDT 2007


On 3/20/07, Andy Myhr <awmyhr at gmail.com> wrote:
> Hello all, I am having an issue with regular expressions in Perl.  I
> have the following:
>
> $name = "Sun (TM) Enterprise 250 (2 X UltraSPARC-II 296MHz)";
>
> What I want is:
>
> $name = "Sun Enterprise 250";
>
> But if I use this:
>
> $hwname =~ s/ \(\w+\)//g;
>
Try:

   $name =~ s/ \([^)]+?\)//g;

Two points...the string "(2 X UltraSPARC-II 296MHz)" has numbers and
spaces both of which are not word characters, and you definately need
to put the question mark after the plus so your regular expression is
no longer greedy, otherwise you'd match:

   (TM) Enterprise 250 (2 X UltraSPARC-II 296MHz)

Cheers...james

P.S. Keep these easy questions comming it will distract me from perl
memory leaks and other things that are all around painful.


More information about the Raleigh-talk mailing list