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

    Join Date
    Aug 2012
    Posts
    13
    Rep Power
    0

    Regular expression problem


    hi,
    i am struggling to do basic regular expression that matches
    "A1234567", must starts with A and is 8 characters long,
    this is what i have so far:

    PHP Code:
    <?php
    $reg1 
    "/^[A]+[0-9]{8}/";
    $lstring "A1234567";
    if (
    preg_match($reg1,$lstring))
    {
        echo 
    "yes";
    }
    else
    {    
        echo 
    "no";
    }
    ?>

    can some one please help
  2. #2
  3. No Profile Picture
    Banned
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    57
    Rep Power
    0
    Hi,

    I think there's a general misunderstanding regarding the regex syntax. What you've wrote down means:

    a string starting with at least one A (can be more) followed by 8 digits and after that an abritrary number of characters

    If you want a string starting with a single A followed by exactly 7 digits and nothing else, that would be

    /^A\d{7}$/

    You cannot set the string length with regexes, only the count of a specific expression.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    13
    Rep Power
    0
    thank you so much.

    is there another way/s of writting that expression?
  6. #4
  7. No Profile Picture
    Banned
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    57
    Rep Power
    0
    Originally Posted by rageedi
    is there another way/s of writting that expression?
    What do you mean? Of course you can make it longer, for example by replacing the \d with [0-9] or by actually writing down all 7 digits. But this is the shortest and most clean way.

IMN logo majestic logo threadwatch logo seochat tools logo