Thread: Regex Question

    #1
  1. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2008
    Posts
    369
    Rep Power
    55

    Regex Question


    Trying to enhance my knowledge of regular expressions. The following regex is just an excerpt from a credit card validator.


    PHP Code:
    '^(?:4[0-9]{12}(?:[0-9]{3})?......' 
    Trying to understand what the '4' just before [0-9] means.
  2. #2
  3. kill 9, $$;
    Devshed Supreme Being (6500+ posts)

    Join Date
    Sep 2001
    Location
    Shanghai, An tSín
    Posts
    6,897
    Rep Power
    3887
    Originally Posted by studeoc
    Trying to understand what the '4' just before [0-9] means.
    It's just a 4. Nothing special about it at all.
  4. #3
  5. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2008
    Posts
    369
    Rep Power
    55
    It must have a purpose. Based on my knowledge that is saying to match 0-9, exactly 12 of them. The string doesn't have to contain a '4' to validate. So it must have some meaning or purpose.
  6. #4
  7. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,141
    Rep Power
    9398
    Of course it has a purpose: it represents the number 4.

    That's all it does.
  8. #5
  9. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2008
    Posts
    369
    Rep Power
    55
    So is that saying the string has to start with the number 4? Or does it just sit there completely useless and ignored?

    The entire regex is:

    PHP Code:
    "/^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|6011[0-9]{12}|3(?:0[0-5]|[68][0-9])[0-9]{11}|3[47][0-9]{13})$/"

    //2659245865985236    //matches
    //1256 5845 6523 5458 //matches
    //d54532-351313         //doesn't match 
    Im just trying to understand how the "4" comes into play here. It almost seems like I could remove it.
    Last edited by studeoc; May 1st, 2010 at 02:14 PM.
  10. #6
  11. kill 9, $$;
    Devshed Supreme Being (6500+ posts)

    Join Date
    Sep 2001
    Location
    Shanghai, An tSín
    Posts
    6,897
    Rep Power
    3887
    You sure about those matches you've posted? That regexp shouldn't match anything starting with 1 or 2.
  12. #7
  13. CSS & JS/DOM Adept
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jul 2004
    Location
    USA (verifiably)
    Posts
    20,128
    Rep Power
    4304
    Originally Posted by studeoc
    So is that saying the string has to start with the number 4? Or does it just sit there completely useless and ignored?
    Yes, the "4" there at the beginning means the string must match the 4 if the expression up until the first "|" is to match. So that regexp will only match strings that start with 4, 5, 3, or 6011 (and of course there are other requirements defined in the expression too).
    Spreading knowledge, one newbie at a time.

    Check out my blog. | Learn CSS. | PHP includes | X/HTML Validator | CSS validator | Common CSS Mistakes | Common JS Mistakes

    Remember people spend most of their time on other people's sites (so don't violate web design conventions).
  14. #8
  15. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2008
    Posts
    369
    Rep Power
    55
    Gotcha. I thought that all characters had to be in the brackets like [4]. But I guess that is only for character ranges.
  16. #9
  17. CSS & JS/DOM Adept
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jul 2004
    Location
    USA (verifiably)
    Posts
    20,128
    Rep Power
    4304
    The brackets allow you to say the next character(s) can be one of those specified (individual characters and/or ranges of characters), not just a single explicit character.

    Also many regexp implementations include shortcuts for some common character ranges. For example "\d" is the same as "[0-9]" and "\D" is the same as "[^0-9]" (a negative character range).

    Comments on this post

    • ishnid agrees
    Spreading knowledge, one newbie at a time.

    Check out my blog. | Learn CSS. | PHP includes | X/HTML Validator | CSS validator | Common CSS Mistakes | Common JS Mistakes

    Remember people spend most of their time on other people's sites (so don't violate web design conventions).

IMN logo majestic logo threadwatch logo seochat tools logo