regexp(n)
π regexp(n) β the Tcl Built-In Commands girlie π
okay bestie, buckle up, we're doing regular expressions today and honestly? it's giving main character energy. this is the regexp manual page, and by the end of it you're gonna be matching patterns like it's nothing. periodt.
β¨ NAME β¨
regexp β match a regular expression against a string π¦
β¨ SYNOPSIS β¨
regexp ?switches? exp string ?matchVar? ?subMatchVar subMatchVar ...?
β¨ DESCRIPTION β¨
so the whole vibe here: this command figures out whether the regular expression exp matches part or all of string. it returns 1 if it does and 0 if it does not β unless you threw -inline in there, in which case it's a whole different story (keep reading, we get to it π
).
(if you wanna know how the regex matching itself actually works, that's a whole separate manual page β go look at the re_syntax reference page, she's the blueprint β¨)
okay so if you pass extra arguments after string, those get treated as the names of variables where we're gonna drop info about which part(s) of string matched exp. here's the tea:
matchVargets set to the range ofstringthat matched all ofexpβ the whole match, no crumbs left behind π- the first
subMatchVargets the characters instringthat matched the leftmost parenthesized subexpression insideexp - the next
subMatchVargets the characters that matched the next parenthesized subexpression to the right inexp - and so on and so on, down the line π
now: if the initial arguments to regexp start with -, they get treated as switches. here's the full roster, no skips, no cuts:
π OPTIONS π
-aboutβ instead of even trying to match the regex, this returns a list of info about the regular expression itself. first element of the list = a subexpression count. second element = a list of property names describing various attributes of the regex. this one is primarily for debugging purposes, iykyk π-expandedβ turns on the expanded regular expression syntax, where whitespace and comments are just... ignored, like they're not even there π. this is the same as specifying the(?x)embedded option (see there_syntaxmanual page).-indicesβ changes what gets stored in thematchVarandsubMatchVars. instead of storing the matching characters fromstring, each variable gets a list of two decimal strings giving the indices instringof the first and last characters in the matching range. coordinates, not content, bestie π-lineβ enables newline-sensitive matching. by default, newline is a completely ordinary character with no special meaning, no drama, nothing. but 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-linestopand-lineanchor, or the(?n)embedded option (see there_syntaxmanual page). it's the combo meal π-linestopβ changes the behavior of[^bracket expressions and.so they stop at newlines. same as specifying the(?p)embedded option (see there_syntaxmanual page).-lineanchorβ changes the behavior of^and$(the "anchors") so they match the beginning and end of a line respectively. same as specifying the(?w)embedded option (see there_syntaxmanual page). anchors, but make it line-aware β-nocaseβ causes upper-case characters instringto be treated as lower case during the matching process. we do not care about your capital letters here π-allβ causes the regular expression to be matched as many times as possible in the string, and returns the total number of matches found. and heads up: if you use this with match variables, those variables will only contain info for the last match. only the last one. don't say I didn't warn you π-inlineβ causes the command to return, as a list, the data that would otherwise be placed in match variables. when you're using-inline, match variables may not be specified, so pick a lane bestie π«Ά. if used with-all, the list gets concatenated at each iteration, such that a flat list is always returned. for each match iteration, the command appends the overall match data, plus one element for each subexpression in the regular expression. examples, because seeing it >> reading it:
regexp -inline -- {\w(\w)} " inlined "
β in n
regexp -all -inline -- {\w(\w)} " inlined "
β in n li i ne e
-start indexβ specifies a character index offset into the string to start matching the regular expression at. the index value is interpreted in the same manner as theindexargument tostring index. when you use this switch,^will not match the beginning of the line, and\Awill still match the start of the string atindex. if-indicesis specified, the indices will be indexed starting from the absolute beginning of the input string. andindexgets constrained to the bounds of the input string β no wandering off π§--β marks the end of switches. the argument following this one will be treated asexpeven if it starts with a-. the escape hatch, the safety net, the "not me getting into an argument with my own flags" moment π
π¦ the empty-match tea π¦
if there are more subMatchVars than there are parenthesized subexpressions within exp, or if a particular subexpression in exp does not match the string (like, for example, because it was in a portion of the expression that wasn't matched at all), then the corresponding subMatchVar gets set to:
-1 -1if-indiceshas been specified, or- an empty string otherwise
no match, no content, that's the rule π
πΈ EXAMPLES πΈ
1. find the first occurrence of a word starting with foo in a string that is not actually an instance of foobar, and grab the letters following it up to the end of the word into a variable:
regexp {\mfoo(?!bar\M)(\w*)} $string -> restOfWord
and note: the whole matched substring got placed in the variable ->, which is a name chosen to look nice given that we're not actually interested in its contents. it's decorative, it's aesthetic, it's -> π
2. find the index of the word badger (in any case) within a string and store it in the variable location:
regexp -indices {(?i)\mbadger\M} $string location
3. this could also be written as a basic regular expression match (as opposed to using the default syntax of advanced regular expressions) by prefixing the expression with a suitable flag:
regexp -indices {(?ib)\<badger\>} $string location
4. this counts the number of octal digits in a string:
regexp -all {[0-7]} $string
5. 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 split command:
regexp -all -inline {\S+} $string
π SEE ALSO π
re_syntax(n), regsub(n), string(n) β go read these next, they're the friend group π
β KEYWORDS β
match, parsing, pattern, regular expression, splitting, string
and that's the whole page, bestie!! no notes, no skips, every flag accounted for π this was Tcl 8.3, and honestly? iconic behavior. now go match some patterns, you absolute regexp girlie π¦β¨π
π¦ where this page comes from
- alpine 3.23: tcl9-doc 9.0.3-r0 Β· TCL
- alpine 3.24: tcl9-doc 9.0.3-r2 Β· TCL
- fedora 43: tcl-doc 1:9.0.2-1.fc43 Β· TCL AND GPL-3.0-or-later WITH Bison-exception-2.2 AND BSD-3-Clause
- fedora 44: tcl-doc 1:9.0.2-1.fc44 Β· TCL AND GPL-3.0-or-later WITH Bison-exception-2.2 AND BSD-3-Clause
- nixos 26.05: tcl 9.0.3
- nixpkgs unstable: tcl 9.1.0
- Copyright (c) 1998 Sun Microsystems, Inc
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms