reg expr, extract digit from string.

Discussion in 'Perl Misc' started by joel, Mar 18, 2005.

  1. joel

    joel Guest

    Hi,

    I need help with extracting digit from a string.

    example:

    here is the string i want to split into words and digits.

    my $string = "abcd-efgh-ijkl-123.456.789"

    i want

    $word = "abcd-efgh-ijkl";

    and

    $digit = "123.456.789";



    Thanks in advance,

    Joel
     
    joel, Mar 18, 2005
    #1
    1. Advertising

  2. joel

    phaylon Guest

    joel wrote:

    > i want


    What have you tried?

    --
    http://www.dunkelheit.at/

    »Better to reign in hell than to serve in heaven«
    -- John Milton, »Paradise Lost«
     
    phaylon, Mar 18, 2005
    #2
    1. Advertising

  3. (joel) writes:
    >
    > I need help with extracting digit from a string.
    >
    > example:
    >
    > here is the string i want to split into words and digits.
    >
    > my $string = "abcd-efgh-ijkl-123.456.789"
    >
    > i want
    >
    > $word = "abcd-efgh-ijkl";
    >
    > and
    >
    > $digit = "123.456.789";


    You seem to have split it successfully yourself already. What's the
    general pattern for your string and word/digit parts? Does the
    digit part contain only digits and periods, and the word part only
    letters and hyphens?
     
    Arndt Jonasson, Mar 18, 2005
    #3
  4. joel <> wrote:


    > my $string = "abcd-efgh-ijkl-123.456.789"
    > i want
    > $word = "abcd-efgh-ijkl";
    > and
    > $digit = "123.456.789";



    my($word, $digit) = $string =~ /(.*)-(.*)/;


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Mar 18, 2005
    #4
  5. joel wrote:
    >
    > I need help with extracting digit from a string.
    >
    > example:
    >
    > here is the string i want to split into words and digits.
    >
    > my $string = "abcd-efgh-ijkl-123.456.789"
    >
    > i want
    >
    > $word = "abcd-efgh-ijkl";
    >
    > and
    >
    > $digit = "123.456.789";


    $ perl -le'
    my $string = "abcd-efgh-ijkl-123.456.789";
    my ( $word, $digit ) = split /(?<=\D)-(?=\d)/, $string, 2;
    print for $word, $digit;
    '
    abcd-efgh-ijkl
    123.456.789



    John
    --
    use Perl;
    program
    fulfillment
     
    John W. Krahn, Mar 18, 2005
    #5
    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. Fangs
    Replies:
    3
    Views:
    9,846
    darshana
    Oct 26, 2008
  2. Thomas Jollans
    Replies:
    2
    Views:
    409
    Carl Howells
    Feb 2, 2004
  3. Good Night Moon
    Replies:
    9
    Views:
    294
    Rick DeNatale
    Jul 25, 2007
  4. Robin
    Replies:
    1
    Views:
    192
    Tassilo v. Parseval
    Jan 22, 2004
  5. Replies:
    6
    Views:
    106
Loading...

Share This Page