soycharliente Posted June 27, 2007 Share Posted June 27, 2007 A friend gave me this code: $pattern = "/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*(([,]|[, ])\s*\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*)*$/"; He said to just trust him that it works. He wouldn't try and help me understand what's going on. I mean, I'm fine with using it, but I'd like to know what's going on so I can learn. I know the basics of regex but nothing like this. I guess I'm just overwhelmed with all of the groupings. I tried to look for this on-line, but I couldn't find anything remotely similar. I looked for one that I could understand, but it seems like everyone has a different pattern. People disagree on what valid characters are, how long certain parts should be, you should explode the parts and test them independently, etc. I'm not interested in changing my code, just understanding what it does. It looks pretty complicated, but like I said, I only know the basics. Any help or comments are MUCH appreciated. Quote Link to comment Share on other sites More sharing options...
Wildbug Posted June 27, 2007 Share Posted June 27, 2007 E-mail validation by regular expression is incredibly difficult/tedious if you go by the RFC definition and not just by "xxx@yyy.asdf.tld" common addresses. That's why you see such disparity in e-mail matchers. Perl has a nice feature, the /x modifier, that allows non-significant whitespace in regular expressions. This allows you to break up a long or complex expression by putting it on many lines, indenting it, and including comments, therefore rendering it readable. PHP includes this feature, too. $pattern = '/^ # The beginning of the string # The username part: \w+ # one or more word characters ([-+.]\w+)* # ...followed by (a dash, plus, or period and at least one more word character) zero or more times # The "at" symbol: @ # The domain part: \w+ # one or more word characters ([-.]\w+)* # ...followed by (a dash or period and at least one more word character) zero or more times \. # a literal period \w+ # one or more word characters ([-.]\w+)* # ...followed by (a dash or period and at least one more word character) zero or more times # Repeat the whole thing zero or more times seperated by commas and/or spaces: (([,]|[, ])\s*\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*)* $/x'; // The end. I'd change this, though: Should be: (\s*,\s*) instead of ([,]|[, ]) Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.