www.webdeveloper.com
Results 1 to 6 of 6

Thread: Regular expression help

  1. #1
    Join Date
    Mar 2004
    Location
    Nevada, USA
    Posts
    175

    Regular expression help

    I am using this code for checking for a proper phone number.
    Code:
    $string_exp = "^[0-9 .-]+$";
    I would also like to have the string be acceptable if they entered the characters N/A in place of a phone number. Can someone tell me how to change this expression to accept these three additional characters as an "or" statement to the phone number characters?

    I don't fully understand the finer points of regular expressions, and haven't been able to figure this one out.
    Last edited by lkeeney; 09-19-2012 at 03:53 PM.
    Larry

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,148
    You could do it as part of the regex, I suppose (adjusted for use with the preg_*() functions, which you should be using instead of ereg_*()?):
    PHP Code:
    "#^(N/A|[0-9 .-])$#i" 
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Mar 2004
    Location
    Nevada, USA
    Posts
    175
    This did not work, and I don't know why.

    I don't understand the difference between preg_*() and ereg_*()?) functions and when and why I should use one over the other in a php file.

    If someone can direct me to a place where I can learn more about this I would appreciate it.
    Last edited by lkeeney; 09-25-2012 at 12:38 PM.
    Larry

  4. #4
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,148
    I left out a repetition modifier:
    PHP Code:
    <?php
    $txt 
    'N/A';
    $result preg_match('#^(N/A|[0-9 .-]+)$#i'$txt);
    var_dump($result);
    //int(1)
    $txt '123-456 789';
    $result preg_match('#^(N/A|[0-9 .-]+)$#i'$txt);
    var_dump($result);
    //int(1)
    $txt 'fubar';
    $result preg_match('#^(N/A|[0-9 .-]+)$#i'$txt);
    var_dump($result);
    //int(0)
    For more info: http://www.php.net/pcre
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  5. #5
    Join Date
    Mar 2004
    Location
    Nevada, USA
    Posts
    175
    Quote Originally Posted by NogDog View Post
    I left out a repetition modifier:
    PHP Code:
    <?php
    $txt 
    'N/A';
    $result preg_match('#^(N/A|[0-9 .-]+)$#i'$txt);
    var_dump($result);
    //int(1)
    $txt '123-456 789';
    $result preg_match('#^(N/A|[0-9 .-]+)$#i'$txt);
    var_dump($result);
    //int(1)
    $txt 'fubar';
    $result preg_match('#^(N/A|[0-9 .-]+)$#i'$txt);
    var_dump($result);
    //int(0)
    For more info: http://www.php.net/pcre
    Thank you. I will try and study this to see if I can understand it.
    Larry

  6. #6
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,148
    A few things to help understand:

    Parentheses enclose a sub-pattern, which is evaluated first (just like parentheses in PHP) and can have repetition modifiers append after the closing paren (not used in this case).

    "|" is the "or" operator, so it matches if either side of the "|" matches.

    I used "#" as the pattern delimiter, but it can be any of most special characters -- but you then have to escape that character within the pattern with a back-slash if you want that literal character to match. The "i" following the closing "#" makes the whole pattern case-Insensitive, so "N/A" or "n/a" would both match.

    The "^" anchors the pattern to the start of the string, and the "$" to the end of the string.

    [] is used to define a "character class": it matches on any of the characters or ranges of characters it contains. I used a "+" repetition modifier after it, meaning to match on one or more instances of those characters. ("*" would be used if you wanted to match on zero or more instances.)
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles