The Pragmatic Craftsman :: Simplicity from complexity ::

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)

Comments are closed.

Random Quote

Topics

Tags

Archive

Recent Entries

Currently Reading

Shelfari: Book reviews on your book blog

:: The Pragmatic Craftsman recommends

:: The Pragmatic Craftsman book reviews

Info

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

@PragCraftsman on Twitter

SOLID principles come in handy here. Especially, "Open of Extension, Closed for Modification." One of the best principles I've learned. - 14 hours agoI actually try to follow @unclebobmartin's rule all the time. Sort of. I try not to make it worse. But to make it better? Great challenge! - 14 hours agoAlways check a module in cleaner than when you checked it out (via @unclebobmartin in 97 Things Every Progr.). I love the idea! - 14 hours agoI put Code Complete #1 over Martin's book because it has "awaken" me as a programmer. But other than that, Martin's book is the best! - 1 day ago@mcory1 First of all, @unclebobmartin is my hero! Agile Software Development, Principles, Patterns, and Practices is my #2. SOLID rules! :) - 1 day ago