SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Zealot Steveiwonder's Avatar
    Join Date
    Nov 2008
    Posts
    151
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Regualar Expressions.

    Hey All,

    I start to struggle when it comes to Regular Expressions, can anyone point me in the right direction? I've googled Tutorials on them but the ones i've tried don't really seems to be all that good.

    Thanks

    /Steve

  2. #2
    Non-Member
    Join Date
    May 2006
    Posts
    137
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Have you looked at this site? It is very complete: Regular-Expressions.info

    You could also try this 10.99 ebook: Sams Teach Yourself Regular Expressions in 10 Minutes . I read it when I was a complete beginner and helped me a lot. I would recommend this little book first, it is very easy to understand, and then you have more complete info on the site above. Ben Forta - The writer is very easy to understand, I highly recommend it to any Regex beginner, for 11 bucks is a must, you need Adobe Digital Editions installed on your pc (windows only) which is free.

    And, if you need an excellent program (windows only) for creating perfect regular expressions, and actually helps you learn too go for RegexBuddy
    Last edited by internetdominus; Mar 27, 2009 at 11:21.

  3. #3
    SitePoint Zealot
    Join Date
    Dec 2006
    Posts
    182
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    http://regexpal.com/ is a nice site to play with too.

  4. #4
    Non-Member
    Join Date
    May 2006
    Posts
    137
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Nice funkdaddy, BTW Steveiwonder what exactly are you looking to learn about regex right now?

  5. #5
    SitePoint Zealot Steveiwonder's Avatar
    Join Date
    Nov 2008
    Posts
    151
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks alot guys, appreciare all the response and will take a look at all.
    Quote Originally Posted by internetdominus View Post
    Nice funkdaddy, BTW Steveiwonder what exactly are you looking to learn about regex right now?
    I was looking at trying to check the format of date/time but in a very specific manor.. i think.

    I looked on the internet for various stuff available and there was loads, but i wanted to attempt it myself as i've never really used them so i though it was a good place to start.

    I did some up with something but i think its really terrible, so i will keep trying, heres what i got.

    Code PHP:
    //Date Format YYYY/MM/DD HH:MM:SS
     
    $DateTime = "2001/01/31 19:30:00";
    $match = ereg("^20[0-9]{2}/[0-1]{1}[1-9]{1}/[0-3]{1}[0-9]{1} (00|01|02|03|04|05|06|07|08|09|10|11|12|13|14|15|16|17|18|19|20|21|22|23):[0-5]{1}[0-9]:[0-5]{1}[0-9]$", $DateTime);
    I know i have asked the year to always start "20", but thats due to this script being used for future dates... hope it makes sense.

    Let me know your thoughts, i'm open for criticism

    //Steve

  6. #6
    Non-Member
    Join Date
    May 2006
    Posts
    137
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    With your line, this date would match: 2009/15/31 14:11:01, and we only have twelve months.

    Try this instead:

    Code:
    20[0-9]{2}[- /.](0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01]) (2[0-3]|[0-1][0-9]):[0-5][0-9]:[0-5][0-9]

    The above will only match 12 months.

  7. #7
    SitePoint Wizard lorenw's Avatar
    Join Date
    Feb 2005
    Location
    was rainy Oregon now sunny Florida
    Posts
    1,098
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    http://www.addedbytes.com/
    check out their cheat sheets, you will find regex and much more, totally valuable website.

    You can do anything/everything with the regex cheat and it's free.

    Cheers
    What I lack in acuracy I make up for in misteaks

  8. #8
    SitePoint Zealot Steveiwonder's Avatar
    Join Date
    Nov 2008
    Posts
    151
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by internetdominus View Post
    With your line, this date would match: 2009/15/31 14:11:01, and we only have twelve months.

    Try this instead:

    Code:
    20[0-9]{2}[- /.](0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01]) (2[0-3]|[0-1][0-9]):[0-5][0-9]:[0-5][0-9]

    The above will only match 12 months.
    Can you explain the additional bits you added, if your not too busy?

    @lorenw - I will check it out

    Thanks

  9. #9
    SitePoint Wizard
    Join Date
    Mar 2008
    Posts
    1,149
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    [- /.] was added so that your parser would be more forgiving. The date could be separated by a period, a space, or a dash in addition to a forward slash.
    (0[1-9]|1[012]) means that either of the following can match: 0[1-9] (months 01 to 09) or 1[012] (months 10 to 12), although you probably already know this

    However, even with these changes, it is still possible to enter invalid dates (think February and 30/11). To make sure a date actually exists, you will have to get the information matched from the regular expression and test it. (It's possible to create a subpattern to just test for valid dates for each month, but then think leap years.)

  10. #10
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,397
    Mentioned
    61 Post(s)
    Tagged
    0 Thread(s)
    I'd also recommend Mastering Regular Expressions which is a worthwhile read for anyone interested (or wanted to get versed) in regular expressions.
    Salathe
    Software Developer and PHP Manual Author.


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
  •