Small string problem

Discussion in 'Perl Misc' started by PeterT, Sep 4, 2003.

  1. PeterT

    PeterT Guest

    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? ;-)


    --
    petert
     
    PeterT, Sep 4, 2003
    #1
    1. Advertising

  2. PeterT

    Shawn Corey Guest

    PeterT wrote:

    > 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;
     
    Shawn Corey, Sep 4, 2003
    #2
    1. Advertising

  3. PeterT

    Lao Coon Guest

    "PeterT" <> wrote in news:bj7ir8$gq$:

    > 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;
     
    Lao Coon, Sep 4, 2003
    #3
  4. PeterT

    John Bokma Guest

    PeterT wrote:

    > 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

    --
    Kind regards, feel free to mail: mail(at)johnbokma.com (or reply)
    virtual home: http://johnbokma.com/ ICQ: 218175426
    John web site hints: http://johnbokma.com/websitedesign/
     
    John Bokma, Sep 4, 2003
    #4
  5. PeterT

    PeterT Guest

    "Christian Caron" <> wrote in message
    news:bj7kdb$...
    >
    > "Christian Caron" <> wrote in message
    > news:bj7jis$...
    >
    > > 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"

    --
    petert
     
    PeterT, Sep 4, 2003
    #5
  6. PeterT

    PeterT Guest

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

    --
    petert
     
    PeterT, Sep 4, 2003
    #6
  7. PeterT

    PeterT Guest

    "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. ;-)

    --
    petert
     
    PeterT, Sep 4, 2003
    #7
  8. "PeterT" <> writes:
    (snip)

    > 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
    --
    perl -e '$;=q,BllpZllla_nNanfc]^h_rpF,;@;=split//,
    $;;$^R.=--$=*ord for split//,$~;sub _{for(1..4){$=
    =shift;$=--if$=!=4;while($=){print chr(ord($;[$%])
    +shift);$%++;$=--;}print " ";}}_(split//,$^R);q;;'
     
    Ryan Shondell, Sep 4, 2003
    #8
  9. "Christian Caron" <> writes:

    > "Ryan Shondell" <-state.edu> wrote in message
    > news:-state.edu...
    > > "PeterT" <> writes:
    > > (snip)
    > >
    > > > 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.

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

    >
    > 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
    --
    perl -e '$;=q,BllpZllla_nNanfc]^h_rpF,;@;=split//,
    $;;$^R.=--$=*ord for split//,$~;sub _{for(1..4){$=
    =shift;$=--if$=!=4;while($=){print chr(ord($;[$%])
    +shift);$%++;$=--;}print " ";}}_(split//,$^R);q;;'
     
    Ryan Shondell, Sep 4, 2003
    #9
  10. Ryan Shondell <-state.edu> wrote:
    > "Christian Caron" <> writes:



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

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

    But only when the s/// _succeeded_.


    >> (later assigned to $num), and also remove these numbers from
    >> $let.



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


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Sep 4, 2003
    #10
  11. PeterT

    PeterT Guest

    "David Oswald"

    > "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? ;-)

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

    --
    petert
     
    PeterT, Sep 5, 2003
    #11
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Carlos Ribeiro
    Replies:
    11
    Views:
    701
    Alex Martelli
    Sep 17, 2004
  2. MackS
    Replies:
    6
    Views:
    313
    Steven D'Aprano
    Nov 15, 2005
  3. Replies:
    12
    Views:
    523
    Andre Kostur
    Sep 28, 2005
  4. =?Utf-8?B?Q2hpV2hpdGVTb3g=?=

    small problem with ASP 1.1 version problem in IIS6

    =?Utf-8?B?Q2hpV2hpdGVTb3g=?=, Apr 2, 2007, in forum: ASP .Net
    Replies:
    6
    Views:
    383
    =?Utf-8?B?Q2hpV2hpdGVTb3g=?=
    Apr 2, 2007
  5. Singeo
    Replies:
    3
    Views:
    368
    Singeo
    May 27, 2008
Loading...

Share This Page