SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Addict cranjled's Avatar
    Join Date
    Apr 2004
    Location
    ny
    Posts
    382
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Help needed with regex patterns.

    Usually, I'll struggle through something for awhile before seeking help, but regex is just something I can't completely wrap my head around. I'm hoping someone can help! Here's the behaviors I'm looking for:

    1) color hex values: allow only digits, lower/upper-case A-G, and "#".

    2) validate arbitrary number: is number between two given numbers, inclusive of the given numbers?

    3) URLs: validate that the format of arbitrary URLs.

    4) email: validate format of arbitrary email addresses.

    5) phone numbers: remove everything except the "+" and digits 0-9.

    6) latitude/longitude: allow only "-", "." and an arbitrary number of digits on either side of the decimal place.

    I know this is a lot to ask, but without help, I could struggle with this for weeks and still have no success. I've learned my lesson with regex and hope someone's feeling especially helpful today!

    Thanks so much for any assistance!

  2. #2
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Do they have to be a RegExp based solution?
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  3. #3
    SitePoint Addict cranjled's Avatar
    Join Date
    Apr 2004
    Location
    ny
    Posts
    382
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Good question, thanks. They don't have to be, no. I was thinking that would be the best way to validate data that follows a format, but contains arbitrary values. I'm certainly open to other approaches, especially if they save CPU!

  4. #4
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,094
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    Sure I'll give you simpler ones

    1) #[0-9a-fA-f]{3,6}
    2) Don't use regex for this one, just use int comparison (if ($x > 10 && $x < 20))
    3 and 4) please google for those, there are lot's of them out there
    5) \+[0-9]+ (or if you know how many digits it should be, \+[0-9]{n} -- where n is the number of digits it should be
    6) \-?([0-9]*\.[0-9]+|[0-9]+\.[0-9]*)

    The last one is a bit more complicated to make sure they either put something in front of the dot, or put something after the dot, or both. Just a dot won't validate.
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  5. #5
    SitePoint Addict cranjled's Avatar
    Join Date
    Apr 2004
    Location
    ny
    Posts
    382
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks -- these are extremely helpful! I appreciate the patterns as well as the extra information and suggestions.

  6. #6
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    2) as you want to include the number stipulated, slight change:
    PHP Code:
    // set up a test loop nos 1- 10 
    foreach( range(1,10) as $x ){

    // here's your conditional test
    if ($x >= && $x <= 6) echo $x;

    }
    // 2 3 4 5 6 

  7. #7
    SitePoint Addict cranjled's Avatar
    Join Date
    Apr 2004
    Location
    ny
    Posts
    382
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks. I did notice the conditional, but didn't want to seem ungrateful or snarky so I just went ahead and made the adjustment on this end silently...

    ...but it's good of you to make sure, though... appreciate it!

  8. #8
    SitePoint Addict cranjled's Avatar
    Join Date
    Apr 2004
    Location
    ny
    Posts
    382
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just a quick follow-up. Below are the patterns I ended up using; some from this thread, some from elsewhere on the net. I tried a lot of patterns and these were the most robust that I came across. Thanks again for the help.

    Cheers!

    Code:
    // Validate format of email addresses.
    // ^(\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,6})^
    
    // Validate format of phone numbers.
    // ^(?:(?:\+?1\s*(?:[.-]\s*)?)?(?:\(\s*([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9])\s*\)|([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9]))\s*(?:[.-]\s*)?)?([2-9]1[02-9]|[2-9][02-9]1|[2-9][02-9]{2})\s*(?:[.-]\s*)?([0-9]{4})(?:\s*(?:#|x\.?|ext\.?|extension)\s*(\d+))?$^
    
    // Validate format of hexadecimal color values.
    // ^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$^
    
    // Validate format of latitude and longitude.
    // ^\-?([0-9]{1,10}\.[0-9]{1,10}|[0-9]{1,10}\.[0-9]{1,10})^
    
    // Validate format of URLs.
    // /^(https?):\/\/'.'(([a-z0-9$_\.\+!\*\'\(\),;\?&=-]|%[0-9a-f]{2})+'.'(:([a-z0-9$_\.\+!\*\'\(\),;\?&=-]|%[0-9a-f]{2})+)?'.'@)?(?#'.')((([a-z0-9][a-z0-9-]*[a-z0-9]\.)*'.'[a-z][a-z0-9-]*[a-z0-9]'.'|((\d|[1-9]\d|1\d{2}|2[0-4][0-9]|25[0-5])\.){3}'.'(\d|[1-9]\d|1\d{2}|2[0-4][0-9]|25[0-5])'.')(:\d+)?'.')(((\/+([a-z0-9$_\.\+!\*\'\(\),;:@&=-]|%[0-9a-f]{2})*)*'.'(\?([a-z0-9$_\.\+!\*\'\(\),;:@&=-]|%[0-9a-f]{2})*)'.'?)?)?'.'(#([a-z0-9$_\.\+!\*\'\(\),;:@&=-]|%[0-9a-f]{2})*)?'.'$/i


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
  •