A '''regular expression''' defines a pattern in text. This page is about regular expressions in general, not necessarily Tcl [regular Expressions] in Tcl. ** See Also ** [Regular Expressions]: describes Tcl's regular expressions ([ARE%|%advanced regular expressions]) [http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap09.html%|%The Open Group Base Specifications Issue 7, Regular Expressions], AKA [POSIX]: [http://www.regular-expressions.info/%|%regularexpressions.info]: lots of general information, and a thorough tutorial with examples ([CL]: although its imprecisions exasperate). See the [http://regular-expressions.info/examples.html%|%examples] for an extensive listing of regulare expressions for various tasks. [http://en.wikipedia.org/wiki/Regular_language%|%Regular Language, Wikipedia]: a formal definitions of "regular language". [BOOK Mastering Regular Expressions], by [Jeffrey E. F. Friedl]: considered almost the definitive tome on regular expressions, from the Unix '''grep'''(1) command to Tcl and beyond. [http://blog.staffannoteberg.com/2013/01/30/regular-expressions-a-brief-history/%|%Regular Expressions - a brief history], by Staffan Nöteberg, 2013-01-30: [http://www2.imm.dtu.dk/~phbi/files/talks/2012remhsacS.pdf%|%Regular Expression Matching: History, Status, and Challenges], Philip Bille: [http://web.archive.org/web/20120704232743/http://zez.org/article/articleprint/11%|%Regular Expressions explained], Jan Borsodi, 2000-10-30: [http://www.onlamp.com/pub/a/onlamp/2003/08/21/regexp.html%|%Five Habits for Successful Regular Expressions], Tony Stubblebine, 2003-08-21: [http://www.ibm.com/developerworks/aix/library/au-regexp/%|%Know your regular expressions], Michael Stutz, 2007-06-14: features a flexible RE generator [http://www.regexlib.com/%|%regexlib.com]: a community maintained library of regular expressions [http://unicode.org/reports/tr18/%|%Unicode Regular Expressions]: Trippin' balls over at the Unicode Consortium. ** Resources ** [http://regexplorer.sourceforge.net/%|%RegExplorer]: ** Description ** 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: ======none A((b|cc)a)* ====== The following strings are from the infinite set of matches for this expression: ======none 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; [ARE]s 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: One-or-more quantifier (`+`): Similar to `*`, but excluding the case of no repetition. The RE “`(`''re''`)+`” is equivalent to “`(`''re''`)(`''re''`)*`” and “`(`''re''`)*(`''re''`)`”. Optional quantifier (`?`): Also called the zero-or-one quantifier. The RE “`(`''re''`)?`” is equivalent to “`(`''re''`|)`”. Bracket expression (`[[`''chars''`]`): Effectively a shorthand — e.g. `[[abcd]` is equivalent to `(a|b|c|d)` — but often far more compact. Unicode character classes often include thousands of character, so enumerating all of them would be unfeasible. Counted quantifiers (`{`''n''`}` or `{`''m''`,`''n''`}`): Exactly ''n'' occurrencies, or at least ''m'' and at most ''n'' occurrencies, respectively. Can as `?` be reduced to combinations of parentheses and `|`, but requires repeating the core RE the quantifier is applied to at least ''n'' times. AND: Boolean "and", like `|` is boolean "or". Uncommon in regexp engines, and not easily reduced to the fundamental operations, but nonetheless the intersection of two regular languages is again a regular language. The [grammar_fa] package uses `&` to denote this. NOT: Boolean negation. Uncommon in regexp engines, and not easily reduced to the fundamental operations, but nonetheless the complement of a regular language is also a regular language. The [grammar_fa] package uses `!` to denote this. Reversal: Change the direction in which the string is being matched; this may be useful to implement searching backwards in a text editor. There is no common syntax for this, but by hand transforming a regular expression accordingly is typically straightforward. 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: Positive lookahead (`(?=`''re''`)`): The regular expression ''re'' must match the text that follows after this position. This is similar to AND, but different in that ''re1'' and ''re2'' in `((?=`''re1''`)`''re2''`)` are not required to match the same characters; ''re1'' can match a prefix of what ''re2'' matches, or vice versa. Negative lookhead (`(?!`''re''`)`): The regular expression ''re'' must ''not'' match the text that follows after this position. Positive lookbehind (`(?<=`''re''`)`): The regular expression ''re'' must match the text that comes before this position. This is not available in [ARE]s. Negative lookbehind (`(?> Glossary | Concept