lucasdmc has asked for the wisdom of the Perl Monks concerning the following question:

I have a few strings which always has a word, then is proceeded by numbers:

art1012387
stuff458353
other432610
etc...

How can I seperate "art" and "1012387", "stuff" and "458353"...

Many thanks, Luke
  • Comment on How do I split a string which has letters then numbers?

Replies are listed 'Best First'.
(Ovid) Re: How do I split a string which has letters then numbers?
by Ovid (Cardinal) on Jan 17, 2002 at 01:49 UTC
    while (<DATA>) { chomp; my ( $letters, $numbers ) = ( /^(\D+)(\d+)$/ ); print "$letters\t$numbers\n"; } __DATA__ art1012387 stuff458353 other432610

    Now that I stop to think about it, you should check if the match is successful and if you really have letters up front. Changing that makes the following while loop:

    while (<DATA>) { chomp; if ( /^([a-zA-Z]+)(\d+)$/ ) { my ( $letters, $numbers ) = ( $1, $2 ); print "$letters\t$numbers\n"; } else { print "No match: $_\n"; } }

    Cheers,
    Ovid

    Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

Re: How do I split a string which has letters then numbers?
by Juerd (Abbot) on Jan 17, 2002 at 01:46 UTC
    my ($word, $number) = split /(?<=\D)(?=\d)/; my ($word, $number) = /^(\D+)(\d+)$/;
    Change the regexes if your data needs more complex parsing.

    hth

    2;0 juerd@ouranos:~$ perl -e'undef christmas' Segmentation fault 2;139 juerd@ouranos:~$

      If you're using split(), I'd be inclined to do split /(?=\d)/, $_, 2;. It feels faster to me.

      _____________________________________________________
      Jeff[japhy]Pinyan: Perl, regex, and perl hacker.
      s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;

Re: How do I split a string which has letters then numbers?
by nikos (Scribe) on Jan 17, 2002 at 07:26 UTC
    Using regular expressions.
    $str="art1532"; ($a, $b)=$str=~/([a-zA-Z]+)(\d+)/; print "$a --- $b\n";
    The first part consist of a-z, A-Z (at least one letter), if you use "+". Or you can use "*", then it could happen that there are no letters before a number. After letters we should find digits (at least one digit). We save what we found in $a and $b. Print the result. Done. You can find additional information on regular expressions at http://www.perlmonks.com/index.pl?node=Tutorials.
A reply falls below the community's threshold of quality. You may see it by logging in.