Regular Expression

A regular expression is a sequence of characters that can be used as a search pattern. Sometimes referred to as Regex or Regexen.

One use of regular expressions is in search engines.

Special Metacharacters

A complete list of Python regular expression characters can be found here.

Often used special metacharacters include:

.  matches any character except newline
^  matches the start of the string
$  matches the end of the string or just before a newline   
*  matches 0 or more repetitions of the preceding Regex
+  matches 1 or more repetitions of the preceding Regex
-  indicates a range, an example is 1-5
{} repetitions of the previous elements; examples are {3} {2,4} {,4}
[] set of any characters to match; examples are [ab4] [a-z]
() capture group based on a Regex; an example is (a*)
|  either or
\  escape to match special characters; an example is \+
\n newline
\d digit
\s space

Python Example

Python regular expression implementation details can be found here.

# Import the regular expression module.
import re

# Define text against which a regular expression will be used.
text_sample = "This is text to test regular expressions."

# Define a regular expression.
regular_expression = "^This"

# Search the text to see if it begins with specific characters.
result = re.findall(regular_expression, text_sample)
print(result)