A regular expression defines a pattern in text. This page is about regular expressions in general, not necessarily Tcl regular Expressions in Tcl.
Regular expressions were developed by Stephen Kleene in the 1950's for the purpose of specifying a regular language. Many tasks can be performed more simply using string and scan instead of regular expressions. A regular expression often looks like a summary of the various forms that the set of strings it describes may take:
The following strings are from the infinite set of matches for this expression:
A Aba Acca Ababa Abacca Accaba Accacca Abababa
Regular expressions are popular for their linear-time linear-time complexity: when a given string is to be matched against a regular expression, it is possible to do it so that every character in the string is only looked at once. This is attained by compiling the regular expression into a finite automaton — potentially a big chunk of work, but one that only needs to be done once for each regular expression — and then running the automaton with the string as input.
Another important factor is that regular expressions can be used for efficiently searching through a large body of text. A direct implementation of the above would produce an algorithm for matching a string against a regular expression, but most RE implementations, including regexp, play a few tricks internally that make them operate in search mode instead. In order to get matching behaviour (often useful with switch -regexp), one uses the anchors ^ and $ to require that the particular position in the regular expression must correspond to the beginning and end of the string respectively (caveat: sometimes it is beginning and end of line instead; AREs have \A and \Z as alternatives).
Other common extensions to the regular expression syntax, which however doesn't make them any more powerful than the basic set described above, are:
A somewhat intriguing class of such extensions are the constraints, which only match the empty string but refuse to do so unless the material surrounding the position of this match satisfies some condition. Here we find:
The Tcl regexp engine handles lookaheads by compiling and running the constraint RE separately; in this way it is a "hybrid" RE engine.
Another set of usual extensions to the syntax concern submatch extraction and greediness. These mainly become meaningful when regular expressions are used for searching, as there in that case often are several substrings that match a particular RE, and it matters what match is reported.
Many "regular expression" engines also support extensions to the syntax which allow them to go beyond the realm of regular languages. This is most common in backtracking RE engines (such as PCRE and the Perl engine) which ignores the finite automaton theory and instead uses trial and error to find a match, but some have escaped into more general standards.
This is a partial list of tricks. It also assumes some familiarity with finite automata theory, such as knowing what distinguishes an NFA from a DFA, how one runs them, and how one can construct one from the other (all of which is standard material in relevant computer science courses).
Given a match-mode regexp engine, as one would get from running a finite automaton over a string and inspect whether the end state is final, one can run a regular expression re in search mode on it by running .*(re).* in match mode.
[Also explain what one can do to find the match searched for, without going for a full-blown submatch-capable engine.]
As usually defined, finite automata can only answer "yes" or "no", so there's no way to get submatch information out of them.
An extension of the formalism (keeping track of positions within the string corresponding to positions in the regexp, as well as the basic automaton state) can be found in NFAs with Tagged Transitions, their Conversion to Deterministic Automata and Application to Regular Expressions
This is a classic trick.
Given one (ε-free) automaton A1 for matching the regular expression re1 and another (ε-free) automaton A2 for matching the regular expression re2, it is straightforward to construct an automaton A for re1 AND re2 as follows:
What this means in practice is that running A is equivalent to running A1 and A2 simultaneously; each A state is a pair of an A1 state and an A2 state. A accepts a string only if both A1 and A2 would do so.
Assuming you've got a DFA for matching a regular language, this is very straightforward: negate the "final" status for every state. States that were previously accepting will then be non-accepting, and vice versa, so strings that were previously accepted will now be rejected (and vice versa).
The basic idea is the same as for boolean AND. First make an automaton for the main RE, then make so many copies of this that you can simultaneously keep track of the state visavi the man RE and the constraint RE, while adjusting the transitions suitably so that you run both in parallel; the projection onto either axis will however still be automata for the main and constraint respectively RE. Finally modify the sets of initial and final states appropriately for the wanted result.
Basically, the projection of the constraint onto the main RE "axis" would be an ε-transition, but it is not parallel to that axis. Rather, it will take you from being debt-free (having all instances of the constraint satisfied, and the string as a whole thus eligible for acceptance) to a debt of 1 match (preventing the string from being accepted), and in order to become free of this debt you will have to work the automaton until reaching a constraint-accepting state again (kind of like an old platform game, where one might fall down a chute into the underground and then painfully having to work one's way up to the surface again before being allowed to finish the game).
What makes this more complicated than the boolean AND is that several instances of the constraint may be relevant at the same time. Consider for example
where each iteration of the outer parenthesis eats one letter, but the constraint is looking for an a up to four characters ahead. It seems the only way to manage that is to run all possibilities in parallel, which means the "constraint axis" of the composite automaton is labelled not by states of an automaton for matching the constraint RE, but by sets of such states (like in the subset construction — no wonder the Tcl engine prefers to go hybrid here)!
Also, that construction only takes care of one constraint at a time. In order to remove all constraints, it would be necessary to repeat it as many times as there are constraints in the expression! Luckily, constraint REs encountered in practice (such as those for beginning-of-word, look one character back and forth) tend to require only a very small number of states, so it is in fact feasible to use them, even with a pure automaton engine.
AMG: I found a couple regular expression puzzles online that are very good for practicing and improving your regular expression skills. Highly recommended. They're not written in Tcl, but I think creating Tcl versions would be a wonderful project.
[Old discussion, could do with refactoring.]
Okay then - feel free to add information here on the other RE flavors available in Tcl...
LES says that there no other RE flavors available in Tcl. Tcl only uses ARE. What I meant is that regular expressions may be construed as any one (or all) of its several variations, but Regular Expressions only discusses Tcl's ARE. I said that because this wiki discusses many things under several contexts, not necessarily that of Tcl, and I thought it would be good to note that, at least in this case, it is restricted to the context of Tcl. Anyone interested in a different or more ample discussion of Regular Expressions will have to look elsewhere. E.g. on PCRE.