View Single Post
Old 12-04-2007, 11:30 PM   #3 (permalink)
Salathe
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,239
Thanks: 3
Salathe is on a distinguished road
Default

Let me start with thanking you for supplying these regular expressions. I intended to comment on one or two things but got carried away so please accept my apologies for the length of this reply!

Some comments:
  1. Telephone Numbers
    There is no need for the parentheses to group results, since we're just testing for the match rather than bring back the matched groups. If you need to group items (for whatever reason) but don't wish to keep them for back references, then use the (?:) pattern.

    As Wildhoney mentioned, why use {3,3} when you can state an explicit number of repetitions (rather than a range) using {3}.

    The [ ] is not necessary, just use a space.
  2. Emails
    Finally a RegExp which allows the plus symbol usage (e.g., salathe+spam@talkphp.com)! Your pattern states that the address must not start with a digit: that means any other character can be used, making @test@test.com (or even $_+@_.aa) valid addresses (correct me if I'm wrong, but I don't think they should be valid).

    Again there is no need to wrap the @ or . characters with brackets. To accept a literal period/fullstop/dot character, outside of character classes, simply escape it: \.
  3. Postal Codes
    See above, regarding {5,5} and {4,4}, and the same for the non-capturing groups.
  4. Ip Address:
    There are no delimeters around the pattern. Attempting to run this code will throw an error.
  5. Hexadecimal Colors
    For readability, I'd probably prefer to use 0-9 than \d. There's no need for the outer non-matching group that I can see.
  6. Multi-line Comments
    This code will not run as-is, resulting in a warning ("Unknown modifier '*'"). The square brackets are not wanted here -- they denote the beginning and end of a character class definition which isn't what we want.

    Next, the forward slashes for the beginning/end of the comments need to be escaped else they conflict with the pattern delimiters (another warning). The asterisks (*) will also need to be escaped.

    The + beside the opening comment characters is not necessary, and the .+ needs to be made ungreedy else you'll run into problems.

    Finally, this will not work if the comment is truly multi-line. You'll want to use the ms pattern modifiers to make the pattern multi-line and dot-all respectively (the latter meaning the . matches newlines as well).

    Suggestion: #/\*.*?\*/#ms
  7. Dates
    The "date" 99/99/9999 would be accepted. It's probably wiser to use one or more of PHP's date functions, or some other method, if you want to check that the date supplied is an actual, valid, date.

Is devolio.com your site?
__________________
salathe@php.net
Salathe is offline  
Reply With Quote
The Following 10 Users Say Thank You to Salathe For This Useful Post:
Andrew (12-16-2007), codefreek (07-15-2008), devolio (12-06-2007), hello-world (03-02-2009), Matt83 (12-05-2007), Morishani (12-06-2007), Nor (12-05-2007), sketchMedia (05-31-2008), Tanax (12-05-2007), Wildhoney (12-04-2007)