Regular Expressions You Should Know

In computing, regular expressions provide a concise and flexible means for identifying strings of text of interest, such as particular characters, words, or patterns of characters. Regular expressions (abbreviated as regex or regexp, with plural forms regexes, regexps, or regexen) are written in a formal language that can be interpreted by a regular expression processor, a program that either serves as a parser generator or examines text and identifies parts that match the provided specification.

 

Matching a User Name

Pattern

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

Description:

Find the beginning of the string (^), followed by any lowercase letter (a-z), number (0-9), an underscore, or a hyphen. Next, {3,16} specifies that are at least 3 of those characters, but no more than 16. Last, get the end of the string ($).

String that matches:

s0me-usEr_n8me (hyphens and underscores allowed)

String that doesn’t match:

an-Extr3mely_L0ngusername (longer than 16 characters)

 

Matching an Email

Pattern:

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

Description:

Find the beginning of the string (^). Inside the first set of paranthesis, we match one or more lowercase letters, numbers, underscores, dots, or hyphens. (An escaped dot is shown here because a non-escaped dot means any character.) After the paranthesis is an @ symbol. Inside the second 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.com

String that doesn’t match:

john@doe.something (TLD is too long)

 

Matching a URL

Pattern: 

/^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/

String that matches:

http://chromaloop.com/about

String that doesn’t match:

http://google.com/some/file!.html (contains an exclamation point)

 

Matching an HTML Tag

Pattern:

/^<([a-z]+)([^<]+)*(?:>(.*)<\/\1>|\s+\/>)$/ 

String that matches:

<a href=”http://chromaloop.com/”>chromaloop.com</a>

String that doesn’t match:

<img src=”img.jpg” alt=”My image>” /> (attributes can’t contain greater than signs)

 

Conclusion

Examples taken from a longer tutorial first posted on http://net.tutsplus.com/tutorials/other/8-regular-expressions-you-should...
There is some great discussion among some users who have stronger oponions about when and where it's appropriate to use regular expressions for production. For most users though, these are good examples to get you pointed in the right direction, and possibly useful for smaller code projects. Happy coding!

Comments

Post new comment

The content of this field is kept private and will not be shown publicly. If you have a Gravatar account, used to display your avatar.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.

More information about formatting options