Skip to content
Tcl Source Code Artifact [c4acfcc421]
Login

Artifact [c4acfcc421]

Artifact c4acfcc4214dd74ecfc98c42fa2f3b0b7735575751aa93c8e2a7f3eb3679fcfc:



CommandName: regexp ManualSection: n Version: 8.3 TclPart: Tcl TclDescription: Tcl Built-In Commands Links:

Name

regexp - Match a regular expression against a string

Synopsis

::: {.synopsis} ::: [regexp]{.cmd} [switches]{.optarg} [exp]{.arg} [string]{.arg} [matchVar]{.optarg} [subMatchVar subMatchVar]{.optdot} :::

Description

Determines whether the regular expression fIexpfR matches part or all of fIstringfR and returns 1 if it does, 0 if it does not, unless fB-inlinefR is specified (see below). (Regular expression matching is described in the fBre_syntaxfR reference page.)

If additional arguments are specified after fIstringfR then they are treated as the names of variables in which to return information about which part(s) of fIstringfR matched fIexpfR. fIMatchVarfR will be set to the range of fIstringfR that matched all of fIexpfR. The first fIsubMatchVarfR will contain the characters in fIstringfR that matched the leftmost parenthesized subexpression within fIexpfR, the next fIsubMatchVarfR will contain the characters that matched the next parenthesized subexpression to the right in fIexpfR, and so on.

If the initial arguments to fBregexpfR start with fB-fR then they are treated as switches. The following switches are currently supported:

-about : Instead of attempting to match the regular expression, returns a list containing information about the regular expression. The first element of the list is a subexpression count. The second element is a list of property names that describe various attributes of the regular expression. This switch is primarily intended for debugging purposes.

-expanded : Enables use of the expanded regular expression syntax where whitespace and comments are ignored. This is the same as specifying the fB(?x)fR embedded option (see the fBre_syntaxfR manual page).

-indices : Changes what is stored in the fImatchVarfR and fIsubMatchVarfRs. Instead of storing the matching characters from fIstringfR, each variable will contain a list of two decimal strings giving the indices in fIstringfR of the first and last characters in the matching range of characters.

-line : Enables newline-sensitive matching. By default, newline is a completely ordinary character with no special meaning. With this flag, "[^" bracket expressions and "." never match newline, "^" matches an empty string after any newline in addition to its normal function, and "$" matches an empty string before any newline in addition to its normal function. This flag is equivalent to specifying both fB-linestopfR and fB-lineanchorfR, or the fB(?n)fR embedded option (see the fBre_syntaxfR manual page).

-linestop : Changes the behavior of "[^" bracket expressions and "." so that they stop at newlines. This is the same as specifying the fB(?p)fR embedded option (see the fBre_syntaxfR manual page).

-lineanchor : Changes the behavior of "^" and "$" (the "anchors") so they match the beginning and end of a line respectively. This is the same as specifying the fB(?w)fR embedded option (see the fBre_syntaxfR manual page).

-nocase : Causes upper-case characters in fIstringfR to be treated as lower case during the matching process.

-all : Causes the regular expression to be matched as many times as possible in the string, returning the total number of matches found. If this is specified with match variables, they will contain information for the last match only.

-inline : Causes the command to return, as a list, the data that would otherwise be placed in match variables. When using fB-inlinefR, match variables may not be specified. If used with fB-allfR, the list will be concatenated at each iteration, such that a flat list is always returned. For each match iteration, the command will append the overall match data, plus one element for each subexpression in the regular expression. Examples are:

```
regexp -inline -- {\w(\w)} " inlined "
      \(-> in n
regexp -all -inline -- {\w(\w)} " inlined "
      \(-> in n li i ne e
```

-start fIindexfR : Specifies a character index offset into the string to start matching the regular expression at. The fIindexfR value is interpreted in the same manner as the fIindexfR argument to fBstring indexfR. When using this switch, "^" will not match the beginning of the line, and A will still match the start of the string at fIindexfR. If fB-indicesfR is specified, the indices will be indexed starting from the absolute beginning of the input string. fIindexfR will be constrained to the bounds of the input string.

-|- : Marks the end of switches. The argument following this one will be treated as fIexpfR even if it starts with a fB-fR.

If there are more fIsubMatchVarfRs than parenthesized subexpressions within fIexpfR, or if a particular subexpression in fIexpfR does not match the string (e.g. because it was in a portion of the expression that was not matched), then the corresponding fIsubMatchVarfR will be set to "-1 -1" if fB-indicesfR has been specified or to an empty string otherwise.

Examples

Find the first occurrence of a word starting with fBfoofR in a string that is not actually an instance of fBfoobarfR, and get the letters following it up to the end of the word into a variable:

regexp {\mfoo(?!bar\M)(\w*)} $string \-> restOfWord

Note that the whole matched substring has been placed in the variable "->", which is a name chosen to look nice given that we are not actually interested in its contents.

Find the index of the word fBbadgerfR (in any case) within a string and store that in the variable fBlocationfR:

regexp -indices {(?i)\mbadger\M} $string location

This could also be written as a fIbasicfR regular expression (as opposed to using the default syntax of fIadvancedfR regular expressions) match by prefixing the expression with a suitable flag:

regexp -indices {(?ib)\<badger\>} $string location

This counts the number of octal digits in a string:

regexp -all {[0-7]} $string

This lists all words (consisting of all sequences of non-whitespace characters) in a string, and is useful as a more powerful version of the fBsplitfR command:

regexp -all -inline {\S+} $string