SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member
    Join Date
    Jan 2012
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Problem with Regular Expression

    Can anyone help with regular expression for
    ebsu/2009/1128
    This what i came out with but is not working as i expected
    /^ebsu(\/|-)\d{4}(\/|-)\d{4}$/

    Please check and modifield my expression

    thanks

  2. #2
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,406
    Mentioned
    149 Post(s)
    Tagged
    4 Thread(s)
    How do you want it to work?

  3. #3
    SitePoint Member
    Join Date
    Jan 2012
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by guido2004 View Post
    How do you want it to work?
    I'm using preg_match($reg, $_POST['matric']);
    I expect the user to enter his matric number in the following format
    ebsu/2009/1119
    ebsu-2009-1119


    ebsu-Is the starting follow by /forward slash of hypen-. I also expect a year 2009,2010 etc.. follow by four digit number \d{4}

  4. #4
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,406
    Mentioned
    149 Post(s)
    Tagged
    4 Thread(s)
    What about this?
    Code:
    #^ebsu[/-]\d{4}[/-]\d{4}$#
    Of course, it also accepts ebsu-3333/1119 (- and /, and a year far far in the future).

    If you want to check the value of the year more specifically, you could do
    PHP Code:
    preg_match('#^ebsu[/-](\d{4})[/-]\d{4}$#''ebsu-2009/1128'$matches);
    print_r($matches
    It would return
    Code:
    Array
    (
        [0] => ebsu-2009/1128
        [1] => 2009
    )
    and then you can do more specific checks on the value in $matches[1].

  5. #5
    SitePoint Member
    Join Date
    Jan 2012
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by guido2004 View Post
    What about this?
    Code:
    #^ebsu[/-]\d{4}[/-]\d{4}$#
    Of course, it also accepts ebsu-3333/1119 (- and /, and a year far far in the future).

    If you want to check the value of the year more specifically, you could do
    PHP Code:
    preg_match('#^ebsu[/-](\d{4})[/-]\d{4}$#''ebsu-2009/1128'$matches);
    print_r($matches
    It would return
    Code:
    Array
    (
        [0] => ebsu-2009/1128
        [1] => 2009
    )
    and then you can do more specific checks on the value in $matches[1].
    Thanks very much: It does the work for me..


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
  •