For Programmers: Free Programming Magazines  


Home > Archive > PERL Miscellaneous > January 2006 > Stripping numbers from a string









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Stripping numbers from a string
Ninja67

2006-01-30, 7:00 pm

This should be an easy one for you seasoned perl developers.

I've got a string that may or may not begin with one or more numbers.
For example, it might be "Smith, John" or it might be "83Smith, John".

I only want the text portion of the string. How do I make sure that
regardless of which of the above is passed to me, I only get "Smith,
John"?

Thank you.

it_says_BALLS_on_your forehead

2006-01-30, 7:00 pm


Ninja67 wrote:
> This should be an easy one for you seasoned perl developers.
>
> I've got a string that may or may not begin with one or more numbers.
> For example, it might be "Smith, John" or it might be "83Smith, John".
>
> I only want the text portion of the string. How do I make sure that
> regardless of which of the above is passed to me, I only get "Smith,
> John"?



my $string = '83Smith, John';
$string =~ s/\d//g;

Ninja67

2006-01-30, 7:00 pm


it_says_BALLS_on_your forehead wrote:
> Ninja67 wrote:
>
>
> my $string = '83Smith, John';
> $string =~ s/\d//g;


Thanks. I can't believe I didn't think of that.

it_says_BALLS_on_your forehead

2006-01-30, 7:00 pm


Ninja67 wrote:
> it_says_BALLS_on_your forehead wrote:
>


actually, i think the below is faster:

$string =~ tr/0-9//d;

Matt Garrish

2006-01-30, 7:00 pm


"it_says_BALLS_on_your forehead" <simon.chao@fmr.com> wrote in message
news:1138660045.508919.71590@f14g2000cwb.googlegroups.com...
>
> Ninja67 wrote:
>
>
> my $string = '83Smith, John';
> $string =~ s/\d//g;
>


Or more safely:

$string =~ s/^\d+//;

Who knows when you'll run into "83Smith 4th, John"...

Matt


Anno Siegel

2006-01-30, 7:00 pm

it_says_BALLS_on_your forehead <simon.chao@fmr.com> wrote in comp.lang.perl.misc:
>
> Ninja67 wrote:
>
> actually, i think the below is faster:
>
> $string =~ tr/0-9//d;


It probably is, but both do not do what the verbal description asks for:
Deleting "one or more numbers" that might precede a name. Reading
"numbers" as "digits", that would be

s/^[0-9]+//;

Reading "numbers" as "numbers", one would have to ask about separators.

Anno
--
If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.
it_says_BALLS_on_your forehead

2006-01-30, 7:00 pm


Matt Garrish wrote:
> "it_says_BALLS_on_your forehead" <simon.chao@fmr.com> wrote in message
> news:1138660045.508919.71590@f14g2000cwb.googlegroups.com...
>
> Or more safely:
>
> $string =~ s/^\d+//;
>
> Who knows when you'll run into "83Smith 4th, John"...


excellent point. when i read that the OP only wanted the text portion
of the string, i (possibly erroneously) designed a simple regex to
delete all digits from the string, regardless of position. i should
have looked more closely at the OP's first paragraph.

it_says_BALLS_on_your forehead

2006-01-30, 7:00 pm


it_says_BALLS_on_your forehead wrote:
> Matt Garrish wrote:
>
> excellent point. when i read that the OP only wanted the text portion
> of the string, i (possibly erroneously) designed a simple regex to
> delete all digits from the string, regardless of position. i should
> have looked more closely at the OP's first paragraph.


*second paragraph.

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com