Small string problem

P

PeterT

I'm trying to extract the numerical part and the letter part of a short
string.

i.e.

$mix = "6KL" --> $num = "6" and $let = "KL"

I can't find a way to do it with split, or substr

anybody can give me a clue? ;-)
 
S

Shawn Corey

PeterT said:
I'm trying to extract the numerical part and the letter part of a short
string.

i.e.

$mix = "6KL" --> $num = "6" and $let = "KL"

I can't find a way to do it with split, or substr

anybody can give me a clue? ;-)

$mix =~ /(\d+)([A-Za-z]+)/;
$num = $1;
$let = $2;
 
L

Lao Coon

PeterT said:
I'm trying to extract the numerical part and the letter part of a short
string.

i.e.

$mix = "6KL" --> $num = "6" and $let = "KL"

I can't find a way to do it with split, or substr

anybody can give me a clue? ;-)

Read perldoc perlre

E.g.

my ($num,$let) = $mix =~ / ( \d+ ) ( [[:alpha:]]+ ) /x;
 
J

John Bokma

PeterT said:
I'm trying to extract the numerical part and the letter part of a short
string.

i.e.

$mix = "6KL" --> $num = "6" and $let = "KL"

I can't find a way to do it with split, or substr

anybody can give me a clue? ;-)

my ($num, $let) = $mix =~ /^(\d+)([A-Z]+)$/;

note that this matches one or more digits followed by one or more
letters in caps. Use i if mixed case is ok.

If it always has this form: exactly one digit followed by exactly 2
letters you could use substr:

$num = substr($mix, 0, 1);
$let = substr($mix, 1);

IIRC
 
P

PeterT

Christian Caron said:
Easy solution:

($num = $mix) =~ s/[a-zA-Z]//g;
($let = $mix) =~ s/[0-9]//g;

Better solution: ?

Please note my solution will work with mixed numbers and letters ($mix =
'6K8L'). You did not mention if you needed that capacity. Also, will it
contain other characters? What to do with them?

Thanks a lot already, and a good point brought to my attention.

I didn't clarify my problem enough, even to myself.

Sometimes I have one or two digits following, which I don't want with $num
but with $let . The leading number, which is not always there, needs to be
seperated from the following string, which always starts with a letter but
can have numbers at the end.

$mix = "17HD21" --> $num = "17" , $let = "HD21"

possible examples are:

"HN" => " ","HN"

"7HN" => "7","HN"

"132HD3" => "132","HD3"
 
P

PeterT

"Lao Coon"
"PeterT"
I'm trying to extract the numerical part and the letter part of a short
string.

i.e.

$mix = "6KL" --> $num = "6" and $let = "KL"

I can't find a way to do it with split, or substr

anybody can give me a clue? ;-)

Read perldoc perlre

E.g.

my ($num,$let) = $mix =~ / ( \d+ ) ( [[:alpha:]]+ ) /x;

Thanks, I assume that would do it fine, but as I expanded in my reply to
Christian, I haven't looked at all permutations of my problem.
 
P

PeterT

"John Bokma"
"PeterT"
I'm trying to extract the numerical part and the letter part of a short
string.

i.e.

$mix = "6KL" --> $num = "6" and $let = "KL"

I can't find a way to do it with split, or substr

anybody can give me a clue? ;-)

my ($num, $let) = $mix =~ /^(\d+)([A-Z]+)$/;

note that this matches one or more digits followed by one or more
letters in caps. Use i if mixed case is ok.

Ta, similar to the suggestion Lao made, though my problem expanded as
I stated earlier. ;-)
 
R

Ryan Shondell

Sometimes I have one or two digits following, which I don't want with $num
but with $let . The leading number, which is not always there, needs to be
seperated from the following string, which always starts with a letter but
can have numbers at the end.

$mix = "17HD21" --> $num = "17" , $let = "HD21"

possible examples are:

"HN" => " ","HN"

"7HN" => "7","HN"

"132HD3" => "132","HD3"

I think this seems to do what you're looking for:

my ($num, $let) = $mix =~ /^(\d+)?(.*)/;

It appears to work when $mix is any of your examples.


Ryan
 
R

Ryan Shondell

Christian Caron said:
This would work:

($let = $mix) =~ s/^(\d+)//;
$num = $1;

It would catch any numbers (if they are at the beginning of the string) and
put them in $1 (later assigned to $num), and also remove these numbers from
$let.

First, why would you use the search and replace operator (s///), when
you really just want a match? But more importantly, this solution
could have some problems if you have previously put something in $1,
but there are no numbers in your string.

$mix = "132HD5";
($let = $mix) =~ s/^(\d+)//;
$num = $1;

# $num = 132 and $let = HD5

$mix = "YO";
($let = $mix) =~ s/^(\d+)//;
$num = $1;

# $num = 132 and $let = YO
# whoops...


Ryan
 
T

Tad McClellan

^^^^^^^^^^^^^^

But only when the s/// _succeeded_.

this solution
could have some problems if you have previously put something in $1,


It has the same problem if you have _not_ previously put something
in $1, its value will just be undef.

but there are no numbers in your string.


Right.

What matters is not what is in $1.

What matters is whether the match succeeded or not.

It it failed, $1 will have "stale" contents from some earlier
(successful) match, or undef.
 
P

PeterT

"David Oswald"
"PeterT"

As your problem expanded with your understanding of how matching works, you
probably came up with the following regexp already, but if not, here it is:


my ( $num, $let ) = $mix =~ m/(\d+)(.+)/;

This tells the regexp engine to match all digits at the beginning of the
string (one or more), and to match everything else (one or more). I make the
assumption that there will always be digits, and there will always be
something following the digits.

Note that this will fail on the following situation: If the string consists
of "1111", you will get 111 as the number and 1 as the remainder. Why?
Because we've told '.' that it has to match something. You could avoid this
by saying .* instead of .+.

Thanks a lot, which goes to everybody who replied and gave me good advice,
I've succesfully managed to get my script to do what I want.
 

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,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top