1regexp(n) Tcl Built-In Commands regexp(n)
2
3
4
5______________________________________________________________________________
6
8 regexp - Match a regular expression against a string
9
10
12 regexp ?switches? exp string ?matchVar? ?subMatchVar subMatchVar ...?
13_________________________________________________________________
14
15
17 Determines whether the regular expression exp matches part or all of
18 string and returns 1 if it does, 0 if it doesn't, unless -inline is
19 specified (see below). (Regular expression matching is described in
20 the re_syntax reference page.)
21
22 If additional arguments are specified after string then they are
23 treated as the names of variables in which to return information about
24 which part(s) of string matched exp. MatchVar will be set to the range
25 of string that matched all of exp. The first subMatchVar will contain
26 the characters in string that matched the leftmost parenthesized subex‐
27 pression within exp, the next subMatchVar will contain the characters
28 that matched the next parenthesized subexpression to the right in exp,
29 and so on.
30
31 If the initial arguments to regexp start with - then they are treated
32 as switches. The following switches are currently supported:
33
34 -about Instead of attempting to match the regular expression,
35 returns a list containing information about the regular
36 expression. The first element of the list is a subex‐
37 pression count. The second element is a list of prop‐
38 erty names that describe various attributes of the regu‐
39 lar expression. This switch is primarily intended for
40 debugging purposes.
41
42 -expanded Enables use of the expanded regular expression syntax
43 where whitespace and comments are ignored. This is the
44 same as specifying the (?x) embedded option (see the
45 re_syntax manual page).
46
47 -indices Changes what is stored in the subMatchVars. Instead of
48 storing the matching characters from string, each vari‐
49 able will contain a list of two decimal strings giving
50 the indices in string of the first and last characters
51 in the matching range of characters.
52
53 -line Enables newline-sensitive matching. By default, newline
54 is a completely ordinary character with no special mean‐
55 ing. With this flag, `[^' bracket expressions and `.'
56 never match newline, `^' matches an empty string after
57 any newline in addition to its normal function, and `$'
58 matches an empty string before any newline in addition
59 to its normal function. This flag is equivalent to
60 specifying both -linestop and -lineanchor, or the (?n)
61 embedded option (see the re_syntax manual page).
62
63 -linestop Changes the behavior of `[^' bracket expressions and `.'
64 so that they stop at newlines. This is the same as
65 specifying the (?p) embedded option (see the re_syntax
66 manual page).
67
68 -lineanchor Changes the behavior of `^' and `$' (the ``anchors'') so
69 they match the beginning and end of a line respectively.
70 This is the same as specifying the (?w) embedded option
71 (see the re_syntax manual page).
72
73 -nocase Causes upper-case characters in string to be treated as
74 lower case during the matching process. │
75
76 -all │
77 Causes the regular expression to be matched as many │
78 times as possible in the string, returning the total │
79 number of matches found. If this is specified with │
80 match variables, they will contain information for the │
81 last match only. │
82
83 -inline │
84 Causes the command to return, as a list, the data that │
85 would otherwise be placed in match variables. When │
86 using -inline, match variables may not be specified. If │
87 used with -all, the list will be concatenated at each │
88 iteration, such that a flat list is always returned. │
89 For each match iteration, the command will append the │
90 overall match data, plus one element for each subexpres‐ │
91 sion in the regular expression. Examples are: │
92 regexp -inline -- {\w(\w)} " inlined " │
93 => {in n} │
94 regexp -all -inline -- {\w(\w)} " inlined " │
95 => {in n li i ne e} │
96
97 -start index │
98 Specifies a character index offset into the string to │
99 start matching the regular expression at. When using │
100 this switch, `^' will not match the beginning of the │
101 line, and \A will still match the start of the string at │
102 index. If -indices is specified, the indices will be │
103 indexed starting from the absolute beginning of the │
104 input string. index will be constrained to the bounds │
105 of the input string.
106
107 -- Marks the end of switches. The argument following this
108 one will be treated as exp even if it starts with a -.
109
110 If there are more subMatchVar's than parenthesized subexpressions
111 within exp, or if a particular subexpression in exp doesn't match the
112 string (e.g. because it was in a portion of the expression that wasn't
113 matched), then the corresponding subMatchVar will be set to ``-1 -1''
114 if -indices has been specified or to an empty string otherwise.
115
117 Find the first occurrence of a word starting with foo in a string that
118 is not actually an instance of foobar, and get the letters following it
119 up to the end of the word into a variable:
120 regexp {\<foo(?!bar\>)(\w*)} $string -> restOfWord
121 Note that the whole matched substring has been placed in the variable
122 -> which is a name chosen to look nice given that we are not actually
123 interested in its contents.
124
125 Find the index of the word badger (in any case) within a string and
126 store that in the variable location:
127 regexp -indices {(?i)\<badger\>} $string location
128
129 Count the number of octal digits in a string:
130 regexp -all {[0-7]} $string
131
132 List all words (consisting of all sequences of non-whitespace charac‐
133 ters) in a string:
134 regexp -all -inline {\S+} $string
135
136
138 re_syntax(n), regsub(n)
139
140
142 match, regular expression, string
143
144
145
146Tcl 8.3 regexp(n)