1regexp(n)                    Tcl Built-In Commands                   regexp(n)
2
3
4
5______________________________________________________________________________
6

NAME

8       regexp - Match a regular expression against a string
9
10

SYNOPSIS

12       regexp ?switches? exp string ?matchVar? ?subMatchVar subMatchVar ...?
13_________________________________________________________________
14
15

DESCRIPTION

17       Determines  whether  the  regular expression exp matches part or all of
18       string and returns 1 if it does, 0 if it does not,  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           Causes the regular expression  to  be  matched  as  many
77                      times  as  possible  in  the string, returning the total
78                      number of matches found.   If  this  is  specified  with
79                      match  variables,  they will contain information for the
80                      last match only.
81
82       -inline        Causes the command to return, as a list, the  data  that
83                      would  otherwise  be  placed  in  match variables.  When
84                      using -inline, match variables may not be specified.  If
85                      used  with  -all,  the list will be concatenated at each
86                      iteration, such that a flat  list  is  always  returned.
87                      For  each  match  iteration, the command will append the
88                      overall match data, plus one element for each subexpres‐
89                      sion in the regular expression.  Examples are:
90                      regexp -inline -- {\w(\w)} " inlined "
91                             in n
92                      regexp -all -inline -- {\w(\w)} " inlined "
93                             in n li i ne e
94
95       -start index   Specifies  a  character  index offset into the string to
96                      start matching the regular  expression  at.   The  index
97                      value  is  interpreted  in  the same manner as the index
98                      argument to string index.  When using this  switch,  “^”
99                      will  not  match  the beginning of the line, and \A will
100                      still match the  start  of  the  string  at  index.   If
101                      -indices  is  specified,  the  indices  will  be indexed
102                      starting  from  the  absolute  beginning  of  the  input
103                      string.   index will be constrained to the bounds of the
104                      input string.
105
106       --             Marks the end of switches.  The argument following  this
107                      one will be treated as exp even if it starts with a -.
108
109       If there are more subMatchVars than parenthesized subexpressions within
110       exp, or if a particular subexpression in exp does not match the  string
111       (e.g.  because  it  was  in  a  portion  of the expression that was not
112       matched), then the corresponding subMatchVar will be set to “-1 -1”  if
113       -indices has been specified or to an empty string otherwise.
114

EXAMPLES

116       Find  the first occurrence of a word starting with foo in a string that
117       is not actually an instance of foobar, and get the letters following it
118       up to the end of the word into a variable:
119              regexp {\mfoo(?!bar\M)(\w*)} $string -> restOfWord
120       Note  that  the whole matched substring has been placed in the variable
121->”, which is a name chosen to look nice given that we are  not  actu‐
122       ally interested in its contents.
123
124       Find  the  index  of  the word badger (in any case) within a string and
125       store that in the variable location:
126              regexp -indices {(?i)\mbadger\M} $string location
127       This could also be written as a basic regular expression (as opposed to
128       using the default syntax of advanced regular expressions) match by pre‐
129       fixing the expression with a suitable flag:
130              regexp -indices {(?ib)\<badger\>} $string location
131
132       This counts the number of octal digits in a string:
133              regexp -all {[0-7]} $string
134
135       This lists all words (consisting of  all  sequences  of  non-whitespace
136       characters)  in  a  string, and is useful as a more powerful version of
137       the split command:
138              regexp -all -inline {\S+} $string
139

SEE ALSO

141       re_syntax(n), regsub(n), string(n)                                      │
142

KEYWORDS

144       match, parsing, pattern, regular expression, splitting, string
145
146
147
148Tcl                                   8.3                            regexp(n)
Impressum