RegEx Can Be Your Friend

This article is a work in progress to create a simple guide to RegEx and some useful examples. After many web searches, it seems that a simple guide that builds up to advanced RegEx is not easily found. Hopefully this will become a great one-stop place to learn about RegEx and begin using it successfully.

Regular expressions (RegEx) provide a means for identifying strings of text, such as particular characters, words, or patterns of characters.

Basic RegEx Operators


. any character

\ Escapes a character that has a special meaning. Thus, \. means a literal . character. Additionally, placing \ in front of a regular character can add a special meaning to that character. For example, \t indicates a tab character.

^ An anchor that insists the pattern start at the beginning of the string. ^A means that the string must start with A.

$ An anchor that insists the string end with the specified pattern. X$ means that the string must end with X.

+ Matches the previous construct one or more times. For example, a+ means "one or more 'a's."

* Matches the previous construct zero or more times. This is the same as +, except that it's also acceptable if the thing wasn't there at all.

( ) Provides grouping and capturing functions. Grouping means treating two or more characters as though they were a single unit. Capturing means remembering the thing that matched, so that we can use it again later. This is called a backreference.

| = Or (backslashed | is a literal pipe)

? Matches the previous construct zero or one times. In other words, make it optional. It also makes the * and + characters "non-greedy." Everything in parens ended by a ? is optional.

[:punct:] - match all punctuation.

[:blank:] - match character space and tab

.* - match anything

.*? - match anything ungreedy

mod_rewrite Target or Substitution

The target of a RewriteRule in mod_rewrite may begin with a specified domain name (e.g., http://w303.com…) or it may be relative and start with a /. If a slash is being used to begin the rewrite target, the server path is the default and a flag must be used to have it interpreted as a URL path.

Basic Syntax

Basic Regular expressions (cheatsheet)

Resources

Tags: ,
If you like this post and would like to receive updates from this blog, please subscribe our feed. Subscribe via RSS

Leave a Reply