phone number backtracking

Discussion in 'Perl Misc' started by jtbutler78@comcast.net, Jun 11, 2007.

  1. Guest

    I was wondering if anyone knew how to get the digits out of a phone
    number where the area code is optional. I have a feeling that there
    is backtracking involved but I am not sure. Anyone have any ideas?

    This is what I have so far - it works for 10 digits not 7.
    $phone = '123-123-1234';
    $phone = '123-1234';

    ($area_cd,$prefix,$extension) = $phone =~ /(?<=(\d{3}) \-) (\d{3}) \-
    (\d{4}) /x;
     
    , Jun 11, 2007
    #1
    1. Advertising

  2. wrote:
    > I was wondering if anyone knew how to get the digits out of a phone
    > number where the area code is optional. I have a feeling that there
    > is backtracking involved but I am not sure. Anyone have any ideas?
    >
    > This is what I have so far - it works for 10 digits not 7.
    > $phone = '123-123-1234';
    > $phone = '123-1234';
    >
    > ($area_cd,$prefix,$extension) = $phone =~ /(?<=(\d{3}) \-) (\d{3}) \-
    > (\d{4}) /x;


    What you tried is called a zero-width positive look-behind assertion. I
    can't see why that would be needed; simple non-capturing parentheses and
    the ? quantifier should do.

    Try:

    /(?:(\d{3})-)?(\d{3})-(\d{4})/;

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Jun 11, 2007
    #2
    1. Advertising

  3. Guest

    On Jun 11, 10:34 am, Gunnar Hjalmarsson <> wrote:
    > wrote:
    > > I was wondering if anyone knew how to get the digits out of a phone
    > > number where the area code is optional. I have a feeling that there
    > > is backtracking involved but I am not sure. Anyone have any ideas?

    >
    > > This is what I have so far - it works for 10 digits not 7.
    > > $phone = '123-123-1234';
    > > $phone = '123-1234';

    >
    > > ($area_cd,$prefix,$extension) = $phone =~ /(?<=(\d{3}) \-) (\d{3}) \-
    > > (\d{4}) /x;

    >
    > What you tried is called a zero-width positive look-behind assertion. I
    > can't see why that would be needed; simple non-capturing parentheses and
    > the ? quantifier should do.
    >
    > Try:
    >
    > /(?:(\d{3})-)?(\d{3})-(\d{4})/;
    >
    > --
    > Gunnar Hjalmarsson
    > Email:http://www.gunnar.cc/cgi-bin/contact.pl


    Thanks. I actually found something similar after I posted this. You
    solution is better then the one I can up with.
     
    , Jun 11, 2007
    #3
    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. Zahoor
    Replies:
    1
    Views:
    827
    Darryl L. Pierce
    Nov 15, 2003
  2. Steven Spear

    Backtracking Search Function Trouble

    Steven Spear, Nov 23, 2004, in forum: C++
    Replies:
    2
    Views:
    379
    Smitsky
    Nov 25, 2004
  3. shoo
    Replies:
    3
    Views:
    453
    Howard
    Mar 8, 2005
  4. chen selina
    Replies:
    0
    Views:
    898
    chen selina
    Jul 7, 2010
  5. Rene Ivon Shamberger

    convert Phone Letters to Phone numbers

    Rene Ivon Shamberger, Dec 4, 2012, in forum: C++
    Replies:
    5
    Views:
    405
Loading...

Share This Page