regexp(3tcl)
π regexp β the Tcl Built-In Command glow-up π
okay bestie, listen. this one's a Tcl Built-In Commands classic and it's lowkey the moment. it's called regexp, and it's about matching a regular expression against a string. periodt.
β¨ SYNOPSIS β¨
regexp ?switches? exp string ?matchVar? ?subMatchVar subMatchVar ...?
πΈ DESCRIPTION πΈ
so what it does: it figures out whether the regular expression exp matches part or all of string, and it hands you back 1 if it does, 0 if it does not β unless you passed -inline, in which case it does something else (scroll down babe, we get there). (and if you wanna know how regex matching itself works, that's all described over in the re_syntax reference page, iykyk.)
now here's the cute part π«Ά β if you drop extra arguments after string, those get treated as the names of variables where the command will stuff info about which part(s) of string matched exp.
MatchVargets set to the range ofstringthat matched all ofexp.- 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, on and on, forever and ever π«
and if the initial arguments to regexp start with -, they're treated as switches, obviously. the currently supported switches are:
π OPTIONS π
-aboutβ instead of even trying to match the regex, it returns a list containing info about the regular expression. first element of that list is a subexpression count. second element is a list of property names describing various attributes of the regex. this one is mainly for debugging purposes, ngl.-expandedβ turns on the expanded regular expression syntax, where whitespace and comments are just ignored. same thing as specifying the(?x)embedded option (peep there_syntaxmanual page).-indicesβ changes what gets stored in thematchVarandsubMatchVars. instead of storing the actual matching characters fromstring, each variable gets a list of two decimal strings: the indices instringof the first and last characters in the matching range of characters. iconic for when you just want positions.-lineβ enables newline-sensitive matching. by default, newline is a completely ordinary character with zero special meaning. with this flag tho:β[^βbracket expressions andβ.βnever match newlineβ^βmatches an empty string after any newline, in addition to its normal functionβ$βmatches an empty string before any newline, in addition to its normal function
this flag is equivalent to specifying both -linestop and -lineanchor, or the (?n) embedded option (again, re_syntax manual page π
).
-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).-nocaseβ makes upper-case characters instringget treated as lower case during matching. slay.-allβ makes the regular expression match as many times as possible in the string, and returns the total number of matches found. if you use this with match variables, they'll only contain info for the last match. just the last one. ok bestie, noted π-inlineβ makes the command return, as a list, the data that would otherwise go into match variables. when you use-inline, match variables may not be specified. if you use it with-all, the list gets concatenated at each iteration, so you always get a flat list back. for each match iteration, the command appends the overall match data, plus one element for each subexpression in the regular expression. examples:
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 where matching the regular expression should start. the index value gets interpreted the same way as the index argument tostring index. when you use this switch,β^βwill not match the beginning of the line, and\Awill still match the start of the string at index. if-indicesis specified, the indices will be indexed starting from the absolute beginning of the input string. index gets constrained to the bounds of the input string. no out-of-bounds nonsense here β--β marks the end of switches. the argument right after this one gets treated asexpeven if it starts with a-. lifesaver fr.
β οΈ heads up bestie: if there are moresubMatchVarsthan parenthesized subexpressions withinexp, or if a particular subexpression inexpdoesn't match the string (like, because it was in a portion of the expression that wasn't matched), then the correspondingsubMatchVargets set toβ-1 -1βif-indiceswas specified, or to an empty string otherwise. that's just how it is π
π EXAMPLES π
find the first occurrence of a word starting with foo in a string that isn't actually an instance of foobar, and get the letters following it up to the end of the word into a variable:
regexp {\mfoo(?!bar\M)(\w*)} $string -> restOfWord
not me putting the whole matched substring into the variable β->β β that's a name chosen purely to look nice, since we are not actually interested in its contents π
find the index of the word badger (in any case) within a string and store that in the variable location:
regexp -indices {(?i)\mbadger\M} $string location
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
this counts the number of octal digits in a string:
regexp -all {[0-7]} $string
and this lists all words (consisting of all sequences of non-whitespace characters) in a string β useful as a more powerful version of the split command:
regexp -all -inline {\S+} $string
π SEE ALSO π
re_syntax(3tcl), regsub(3tcl), string(3tcl)
π¦ KEYWORDS π¦
match, parsing, pattern, regular expression, splitting, string
okay that's the whole page, bestie, we ate and left no crumbs π
β¨ this was the regexp(3tcl) page from Tcl 8.3 β go match something iconic, you've got this π«Άπ
π¦ where this page comes from
- debian trixie: tcl9.0-doc 9.0.1+dfsg-2
- ubuntu questing: tcl9.0-doc 9.0.2+dfsg-1
- ubuntu resolute: tcl9.0-doc 9.0.3+dfsg-1
- ubuntu stonking: tcl9.0-doc 9.0.4+dfsg-1
- 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