#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    11
    Rep Power
    0

    Question Regular expressions


    Trying to learn regular expression.
    Can any body tell me how do extract only the name and phone number from the below string.

    Norma Corder:3978572735:74 Pine Street, Dearborn, MI


    Thanks for your help in advance
  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Location
    spaceBAR Central
    Posts
    229
    Rep Power
    42
    This is a simple example using a regular expression:
    Code:
    $ cat t
    Norma Corder:397-857-2735:74 Pine Street, Dearborn, MI
    
    $ perl -lne 'print "$1 $2" if /^(.+):(\d{3}-\d{3}-\d{4}):/' t
    Norma Corder 397-857-2735
    But you could use a 'substr' to also get the info.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Posts
    835
    Rep Power
    496
    Or just use the split function:

    Code:
    my $line = 'Norma Corder:397-857-2735:74 Pine Street, Dearborn, MI';
    my ($name, $tel_number, undef) = split /:/, $line, 3;
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Apr 2009
    Posts
    1,940
    Rep Power
    1225
    If using split instead of a regex, I'd use an array slice.
    Code:
    my ($name, $tel_number) = (split /:/, $line)[0,1];

IMN logo majestic logo threadwatch logo seochat tools logo