An Overview of Regular Expressions

Nichole Shelton

December 30, 2014

Regular expressions are a way to describe a set of strings based on common characteristics shared by each stirng in the set. They can be used to search, edit, or manipulate text or data (2).Regular expressions were invented or defined by an American mathemetician Stephen Kleene(1).

Regex has a specific syntax. They're helpful in extracting info from text such as code, log files, spreadsheets, or documents (3).

Similar to Ruby's "everything is an object", everything in regex is a character. All letters of the alphabet, numbers, and special characters like @$!# are all considered single charachters. You can write your own patterns to match specific sequences of characters (ie: a string!).

Regex is a string. Just like three, chicken, and '423' are strings so is [B-R]+:.

Regex is used for 4 main reasons: finding text within a larger body of text, validating a string conforms to a certain format, replacing or inserting text, and splitting strings. A regex engine exists for text editors and most programming languages (3).

Here are some useful definitions that will help you think about how regular expressions are used (1):

literal: A literal is any character we use in a search or matching expression, for example, to find ind in windows the ind is a literal string - each character plays a part in the search, it is literally the string we want to find.

metacharcter: A metacharacter is one or more special characters that have a unique meaning and are NOT used as literals in the search expression, for example, the character ^ (circumflex or caret) is a metacharacter.

target string: This term describes the string that we will be searching, that is, the string in which we want to find our match or search pattern.

search expression: Most commonly called the regular expression. This term describes the search expression that we will be using to search our target string, that is, the pattern we use to find what we want.

escape sequence: An escape sequence is a way of indicating that we want to use one of our metacharacters as a literal. In a regular expression an escape sequence involves placing the metacharacter \ (backslash) in front of the metacharacter that we want to use as a literal, for example, if we want to find (s) in the target string window(s) then we use the search expression \(s\) and if we want to find \\file in the target string c:\\file then we would need to use the search expression \\\\file (each \ we want to search for as a literal (there are 2) is preceded by an escape sequence \).

Check out this little key to get you started on using regular expressions (4):

^     Start of a string.
$     End of a string.
.     Any character (except \n newline)
|     Alternation.
{...} Explicit quantifier notation.
[...] Explicit set of characters to match.
(...) Logical grouping of part of an expression.
*     0 or more of previous expression.
+     1 or more of previous expression.
?     0 or 1 of previous expression; also forces minimal matching when an expression might match several strings within a search string.
\     Preceding one of the above, it makes it a literal instead of a special character. Preceding a special matching character, see below.

This barely skimmed the surface of regular expressions. Here are a couple of tutorials I've used throughout this introductory post that can help provide you with more in depth information:
http://regexone.com/
http://www.rexegg.com/

Resources:
1) http://www.zytrax.com/tech/web/regex.htm
2) http://docs.oracle.com/javase/tutorial/essential/regex/intro.
3) http://regexone.com/
4) http://regexlib.com/CheatSheet.aspx?AspxAutoDetectCookieSupport=1

Good luck and happy coding!