Stripping numbers from a string

N

Ninja67

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.
 
I

it_says_BALLS_on_your forehead

Ninja67 said:
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;
 
I

it_says_BALLS_on_your forehead

actually, i think the below is faster:

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

Matt Garrish

it_says_BALLS_on_your forehead said:
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
 
A

Anno Siegel

it_says_BALLS_on_your forehead said:
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
 
I

it_says_BALLS_on_your forehead

Matt said:
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.
 
I

it_says_BALLS_on_your forehead

it_says_BALLS_on_your forehead said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top