# Tcl Programming/regexp
### Overview

Another language (I wouldn't want to call it "little") embedded inside Tcl is regular expressions. They may not look like this to you - but they follow (many) rules, indeed. The purpose is to describe a string pattern to match with - for searching, extracting, or replacing substrings.
Regular expressions are used in the regexp, regsub commands, and optionally in lsearch and switch. Note that this language is very different from Tcl itself, so in most cases it is best to brace an RE, to prevent the Tcl parser from misunderstanding them.
Before the gory details begin, let's start with some examples:
returns 1 if $input contains one or more digits, followed by a lowercase letter.
deletes all uppercase letters from $input, and saves that to the result variable.
returns all elements in the list $input which start with a "-".

### Character classes

Many characters just stand for themselves. E.g.
matches indeed the character "a". Any Unicode can be specified in the \uXXXX format. In brackets (not the same as Tcl's), a set of alternatives (a "class") is defined:
matches "a", "b", or "c". A dash (-) between two characters spans the range between them, e.g.
matches one decimal digit. To have literal "-" in a set of alternatives, put it first or last:
matches a digit or a minus sign. A bracketed class can be negated by starting it with ^, e.g.
matches any character that is not a decimal digit. The period "." represents one instance of any character. If a literal "." is intended (or in general, to escape any character that has a special meaning to the regexp syntax), put a backslash "\" before it - and make sure the regular expression is braced, so the Tcl parser doesn't eat up the backslash early...).

### Quantifiers

To repeat a character (set) other than once, there are quantifiers put behind it, e.g.
There is also a way of numeric quantification, using braces (again, not the same as Tcl's):
The + and * quantifiers act "greedy", i.e. they consume the longest possible substring. For non-greedy behavior, which provides the shortest possible match, add a "?" in behind. Examples:
This matches until the last close-bracket
This matches until the first close-bracket.

### Anchoring

By default, a regular expression can match anywhere in a string. You can limit that to the beginning (^) and/or end ($):
succeeds if input begins with "a" and ends with "z", and has one or more characters between them.

### Grouping

A part of a regular expression can be grouped by putting parentheses around it. This can have several purposes:
* regexp and regsub can extract or refer to such substrings
* the operator precedence can be controlled
The "|" (or) operator has high precedence. So
matches the strings "foo" or "bar grill", while
matches "foo grill" or "bar grill".
For extracting substrings into separate variables, regexp allows additional arguments:
Here, variable fullmatch will receive the substring of input that matched the regular expression re, while part1 etc. receive the parenthesized submatches. As fullmatch is often not needed, it has become an eye-candy idiom to use the variable name "->" in that position, e.g.
places the first two characters of input in the variable first, and the next three in the variable second. If $input was "ab123", first will hold "ab", and second will contain "123".
In regsub and regexp, you can refer to parenthesized submatches with \1 for the first, \2 for the second, etc. \0 is the full match, as with regexp above. Example:
Here \1 contains "ab", \2 contains "123", and \0 is the full match "ab123". Another example, how to find four times the same lowercase letter in a row (the first occurrence, plus then three):

### More examples

Parse the contents in angle brackets (note that the result contains the full match and the submatch in paired sequence, so use foreach to extract only the submatches):
Insert commas between groups of three digits into the integer part of a number:
In other countries, you might use an apostrophe (') as separator, or make groups of four digits (used in Japan).
In the opposite direction, converting such formatted numbers back to the regular way for use in calculations, the task consists simply of removing all commas. This can be done with regsub:
but as the task involves only constant strings (comma and empty string), it is more efficient not to use regular expressions here, but use a string map command: