SitePoint Sponsor

User Tag List

Results 1 to 13 of 13
  1. #1
    SitePoint Evangelist N9ne's Avatar
    Join Date
    Aug 2002
    Location
    UK
    Posts
    596
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Very small regex question...

    What is the regex code for a 4 digit figure, with the first figure being 4, and the next 3 being any digit?

  2. #2
    eschew sesquipedalians silver trophy sweatje's Avatar
    Join Date
    Jun 2003
    Location
    Iowa, USA
    Posts
    3,749
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
     $regex '/4\d{3}/'
    not tested
    Jason Sweat ZCE - jsweat_php@yahoo.com
    Book: PHP Patterns
    Good Stuff: SimpleTest PHPUnit FireFox ADOdb YUI
    Detestable (adjective): software that isn't testable.

  3. #3
    SitePoint Evangelist N9ne's Avatar
    Join Date
    Aug 2002
    Location
    UK
    Posts
    596
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't think it's working...

  4. #4
    La la la la la bronze trophy lieut_data's Avatar
    Join Date
    Jun 2003
    Location
    Waterloo, ON
    Posts
    1,517
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Perhaps you want it to match 100% (i.e. nothing else in the source?):

    PHP Code:
    $source '4314';
    $regex '/^4\d{3}$/';

    if (
    preg_match($regex$source)) {
       echo 
    'Matched!';
    } else {
       echo 
    'Didn\'t match!';

    Edit:

    as per mattjacob's helpful suggestions
    Last edited by lieut_data; Sep 10, 2003 at 13:58.
    My name is Steve, and I'm a super-villian.

  5. #5
    SitePoint Addict CeleronXL's Avatar
    Join Date
    Dec 2002
    Location
    United States of America
    Posts
    349
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $regEx '/^4[0-9]{3}$/'
    So hold me when I'm here
    Right me when I'm wrong
    Hold me when I'm scared
    Love me when I'm gone.

  6. #6
    "Of" != "Have" bronze trophy Jeff Lange's Avatar
    Join Date
    Jan 2003
    Location
    Calgary, Canada
    Posts
    2,063
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    ^4\d{3}$
    should work...
    Who walks the stairs without a care
    It shoots so high in the sky.
    Bounce up and down just like a clown.
    Everyone knows its Slinky.

  7. #7
    SitePoint Wizard
    Join Date
    Oct 2001
    Location
    Tucson, Arizona
    Posts
    1,858
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yep, that'll work just fine.

    lieut_data, not to be nitpicky, but your example code had a few errors in it. I corrected it below:
    PHP Code:
    $source '4314';
    $regex '/^4\d{3}$/';

    if (
    preg_match($regex$source))
    {
        echo 
    'Matched!';
    }
    else 
    {
        echo 
    'Didn\'t match!';


  8. #8
    SitePoint Evangelist N9ne's Avatar
    Join Date
    Aug 2002
    Location
    UK
    Posts
    596
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for all the help guys .

  9. #9
    La la la la la bronze trophy lieut_data's Avatar
    Join Date
    Jun 2003
    Location
    Waterloo, ON
    Posts
    1,517
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by mattjacob
    Yep, that'll work just fine.

    lieut_data, not to be nitpicky, but your example code had a few errors in it. I corrected it below:
    PHP Code:
    $source '4314';
    $regex '/^4\d{3}$/';

    if (
    preg_match($regex$source))
    {
        echo 
    'Matched!';
    }
    else 
    {
        echo 
    'Didn\'t match!';

    I'll be the first to admit an error of mine -- but besides the single quote at the bottom, what did I miss?
    My name is Steve, and I'm a super-villian.

  10. #10
    eschew sesquipedalians silver trophy sweatje's Avatar
    Join Date
    Jun 2003
    Location
    Iowa, USA
    Posts
    3,749
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by lieut_data
    I'll be the first to admit an error of mine -- but besides the single quote at the bottom, what did I miss?
    Must not have liked your bracing style ?!?

  11. #11
    SitePoint Wizard
    Join Date
    Oct 2001
    Location
    Tucson, Arizona
    Posts
    1,858
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Check out this line:
    PHP Code:
    if (preg_match($regex$source) { 
    You were missing a second parenthesis.

  12. #12
    eschew sesquipedalians silver trophy sweatje's Avatar
    Join Date
    Jun 2003
    Location
    Iowa, USA
    Posts
    3,749
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by mattjacob
    You were missing a second parenthesis.
    Good eyes

  13. #13
    La la la la la bronze trophy lieut_data's Avatar
    Join Date
    Jun 2003
    Location
    Waterloo, ON
    Posts
    1,517
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by mattjacob
    Check out this line:
    PHP Code:
    if (preg_match($regex$source) { 
    You were missing a second parenthesis.
    Ok, now I'm going to need glasses!

    Thanks for correcting me

    (I guess this handy brace highlighting in PHP-Edit is making me lazy...)
    My name is Steve, and I'm a super-villian.


Bookmarks

Posting Permissions

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