Shell Problems#193 Valid Phone Numbers


  • 0
    C

    the easy shell problem, look details, just to valid the phone numbers.

    I use regex with egrep, and the result is wrong, with reason:

    Input:
    123-456-7891
    Output:
    Expected:
    123-456-7891
    

    But I test with my shell command:

    egrep -i '(\d{3}-|\(\d{3}\)\s)\d{3}-\d{4}' file.txt  
    

    it works well.

    I donnot know why, who could give me some tips, thanks very much for this.


  • 0

    Wrong category. Moving to General Discussions for now. Will move to the correct category after Discuss migration.


  • 0
    C

    @1337c0d3r I am sorry, thanks.


  • 1

    @chadlwm Please check here if it helps. I think what you want is grep. The script is run in Ubuntu linux environment, so your version of egrep in Mac is probably different from the egrep in Ubuntu.


  • 0
    C

    @1337c0d3r cool, that's the point. the correct command is:

    grep -P '^(\d{3}-|\(\d{3}\)\s)\d{3}-\d{4}$' file.txt 
    

    I test the bash command in mac os, it fails when running in ubuntu env.

    thanks very much.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.