# Regular Expression Syntax

A regular expression is a pattern of text that consists of ordinary characters (for example, letters a through z) and special characters, known as meta-characters. The pattern describes one or more strings to match when searching a body of text. The regular expression serves as a template for matching a character pattern to the string being searched.

A regular expression is often used to find sequences of characters inside a longer string. For example, the original string might be:

"The quick brown fox"

and we want to replace "quick" with "slow".

Here are some examples of regular expression you might develop:

Pattern | Meaning
"q.*k" | Matches any sequence of characters starting with 'q' and ending with 'k'. For example, it matches "qk", "qkk", and "quick". This will find "quick" in the sentence above.
"^q.*k" | Matches any sequence of characters starting with 'a' and ending with 'c', but only if the sequence is at the beginning of the string. For example, it matches "quick" in the sequence "quick brown", but does not match "The quick brown".
"^(.*)quick(.*)$" | This is an advanced expression that divides the input into three sections. the first section is everything from the start of the line to the "quick"; the second section is the "quick"; and the third section is everything from these to the end of the line.
"$1slow$2" | Assuming that we used the expression above to divide the input into sections, this makes a new line. The first section is everything between the first set of brackets; the second section is "slow"; and the third section is everything between the second set of brackets.

Below are the various meta-characters that can be used in regular expressions.

Character | Description
^ | Beginning of a string.
$ | End of a string.
. | Any character.
[list] | Any character in the list. For example, "[AEIOU]" matches any single uppercase vowel.
[^list] | Any character not in the list. For example, "[^XZ]" matches any character except an 'X' or a 'Z'.
[A-Z] | Any character between the first character and the last character. For example, "[0-9]" matches any single digit.
? | Repeat previous character zero or one time. For example, "10?" matches "1" and "10" but not "100".
* | Repeat previous character zero or more times. For example, "10*" matches "1", "10", "1000", etc.
+ | Repeat previous character one or more times. For example, "10+" matches "10", "1000", etc.
\ | Escape next character. This is required to any of the special characters that are part of the syntax. For example "\.\*\+\\" matches ".*+\". It is also required to encode some special non-printable characters (such as tabs) listed below.
(tag) | Tag this part of the match so you can refer to it later using the "$N" sequence below. For example, the pattern "
$N | Matches the Nth occurrence of a tag in the search pattern (N must be a number from 1 to 9)
#N | The contents of field N in the input record. You can use this to test the value of one field, then replace with the contents of one or more different fields. (N must be a number - 1, 2, 23, ...)

In addition to the characters listed above, there are seven special characters encoded using the backslash. These are listed below:

Character | Description
\a | Bell (Chr(7))
\b | Backspace (Chr(8))
\f | Form feed (Chr(12))
\n | New line (Chr(10))
\r | Carriage return (Chr(13))
\t | Horizontal tab (Chr(9))
\v | Vertical tab (Chr(11))

Note that if a pattern can match the string in more than one way, the longest match will prevail. For example, suppose we have the test:

".*,"

which means "any sequence of characters terminating in a comma", and we have an input field with the value:

"one, two, three, four"

Possible matches would be:

"one,"
"one, two,"
"one, two, three,"

In cases like this, the longest possible match will be used; thus the match would be "one, two, three,"