The Pragmatic Craftsman :: Simplicity from complexity : by Stanley Kubasek ::

Learning Regular Expressions

I have to admit, my regex skills are not that sharp. I read a book on Regular Expressions before, but still, regex expressions just don’t stick in my mind. Too cryptic.

Can you read the following?

/^[a-z0-9_-]{3,16}$/

The following explanation might help.

DescriptionWe begin by telling the parser to find the beginning of the string (^), followed by any lowercase letter (a-z), number (0-9), an underscore, or a hyphen. Next, {3,16} makes sure that are at least 3 of those characters, but no more than 16. Finally, we want the end of the string ($).

String that matches: my-us3r_n4m3String that doesn’t match: wayt00_l0ngt0beausername (too long)

Clear, right? I love the description. It makes sense! This is an excerpt from an article 8 Regular Expressions You Should Know.

This one is a bit more complicated.

/^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$/

DescriptionWe begin by telling the parser to find the beginning of the string (^). Inside the first group, we match one or more lowercase letters, numbers, underscores, dots, or hyphens. I have escaped the dot because a non-escaped dot means any character. Directly after that, there must be an at sign. Next is the domain name which must be: one or more lowercase letters, numbers, underscores, dots, or hyphens. Then another (escaped) dot, with the extension being two to six letters or dots. I have 2 to 6 because of the country specific TLD’s (.ny.us or .co.uk). Finally, we want the end of the string ($).

String that matches: john@doe.comString that doesn’t match: john@doe.something (TLD is too long)

Reference8 Regular Expressions You Should Know, nettuts (very good resource, btw)

Related Posts

Favorite Quote

Topics

Tags

Archive

Currently Reading

Info

© 2001-2012 Stanley Kubasek About me :: Contact me

Me on Twitter

The key to performance is elegance, not battalions of special cases. — Jon Bentley and Doug McIlroy - 7 days agoThe ability to simplify means to eliminate the unnecessary so that the necessary may speak. — Hans Hoffmann - 12 days agoSo much complexity in software comes from trying to make one thing do two things. — Ryan Singer - 18 days agoGood code is short, simple, and symmetrical - the challenge is figuring out how to get there. — Sean Parent - 20 days agoSimplicity carried to the extreme becomes elegance. — Jon Frankli - 24 days ago

»see more

Recent Entries