Tcl_RegExpMatch(3tcl)
πΈ Tcl_RegExpMatch β the 3tcl manual page π
okay bestie, listen. this is the Tcl Library Procedures page for regex matching in C, and it is lowkey a whole vibe. it covers Tcl_RegExpMatch, Tcl_RegExpCompile, Tcl_RegExpExec, Tcl_RegExpRange, Tcl_GetRegExpFromObj, Tcl_RegExpMatchObj, Tcl_RegExpExecObj, and Tcl_RegExpGetInfo. grab a strawberry drink, we're doing this π
β¨ NAME β¨
Tcl_RegExpMatch, Tcl_RegExpCompile, Tcl_RegExpExec, Tcl_RegExpRange, Tcl_GetRegExpFromObj, Tcl_RegExpMatchObj, Tcl_RegExpExecObj, Tcl_RegExpGetInfo β pattern matching with regular expressions π¦
π SYNOPSIS π
so here's the lineup, exactly as written, no cap:
#include <tcl.h>
int
Tcl_RegExpMatchObj(interp, textObj, patObj)
int
Tcl_RegExpMatch(interp, text, pattern)
Tcl_RegExp
Tcl_RegExpCompile(interp, pattern)
int
Tcl_RegExpExec(interp, regexp, text, start)
Tcl_RegExpRange(regexp, index, startPtr, endPtr)
Tcl_RegExp
Tcl_GetRegExpFromObj(interp, patObj, cflags)
int
Tcl_RegExpExecObj(interp, regexp, textObj, offset, nmatches, eflags)
Tcl_RegExpGetInfo(regexp, infoPtr)
π ARGUMENTS π
every single one of them, in order, because we do not skip:
Tcl_Interp *interp(in) β the Tcl interpreter you're using to report errors. it is totally fine for this to beNULLif you don't want error reporting, no judgment πTcl_Obj *textObj(in/out) β points at the value you're pulling the text-to-search out of. heads up: the internal representation of that value might get converted into a form that can be searched efficiently. it's giving glow up β¨Tcl_Obj *patObj(in/out) β points at the value you're getting a regular expression from. the compiled regexp gets cached right there inside the value. iconic behavior π«Άconst char *text(in) β the text you're hunting for a match in, using a regular expression.const char *pattern(in) β a string shaped like a regular expression pattern.Tcl_RegExp regexp(in) β a compiled regular expression. it must have been handed to you earlier byTcl_GetRegExpFromObjorTcl_RegExpCompile. no bootleg regexps, periodt.const char *start(in) β iftextis just a slice of some bigger string, this argument marks where that bigger string begins. and if it is not the same astext, then noβ^βmatches will be allowed. important!! π€Tcl_Size index(in) β says which range you want:0means the range of the entire match,1or greater means the range that matched a parenthesized sub-expression.const char **startPtr(out) β the address of the first character in the range gets stashed here, orNULLif there is no such range.const char **endPtr(out) β the address of the character just after the last one in the range gets stashed here, orNULLif there is no such range.int cflags(in) β an OR-ed combination of the compilation flagsTCL_REG_ADVANCED,TCL_REG_EXTENDED,TCL_REG_BASIC,TCL_REG_EXPANDED,TCL_REG_QUOTE,TCL_REG_NOCASE,TCL_REG_NEWLINE,TCL_REG_NLSTOP,TCL_REG_NLANCH,TCL_REG_NOSUB, andTCL_REG_CANMATCH. more info below, keep scrolling πTcl_Size offset(in) β the character offset into the text where matching should begin. plot twist: the value of the offset has no impact on^matches. that behavior is controlled byeflagsinstead πTcl_Size nmatches(in) β how many matching subexpressions should be remembered for later. if this is0, then no subexpression match info gets computed at all. if it's negative, then all of the matching subexpressions get remembered. any other value is taken as the max number of subexpressions to remember. so many options, we love choices πint eflags(in) β an OR-ed combination of the execution flagsTCL_REG_NOTBOLandTCL_REG_NOTEOL. details below πΈTcl_RegExpInfo *infoPtr(out) β the address of the spot whereTcl_RegExpGetInfoshould store info about a previous match.
π DESCRIPTION π
okay so, the main character: Tcl_RegExpMatch figures out whether its pattern argument matches regexp, where regexp is read as a regular expression using the rules in the re_syntax reference page. if there's a match, Tcl_RegExpMatch returns 1. if there's no match, it returns 0. and if something goes wrong during matching β like, say, pattern isn't a valid regular expression β then Tcl_RegExpMatch returns -1 and leaves an error message in the interpreter result. π
Tcl_RegExpMatchObj is basically Tcl_RegExpMatch's cooler sibling: same deal, except it works on the Tcl values textObj and patObj instead of UTF strings. and ngl, Tcl_RegExpMatchObj is generally more efficient than Tcl_RegExpMatch, so it's the preferred interface. bestie, pick the faster one π
π§ the low-level crew: Tcl_RegExpCompile, Tcl_RegExpExec, Tcl_RegExpRange π§
these three give you lower-level access to the regular expression pattern matcher.
Tcl_RegExpCompile compiles a regular expression string into the internal form used for efficient pattern matching. the return value is a token for this compiled form, which you can then use in later calls to Tcl_RegExpExec or Tcl_RegExpRange. if an error happens while compiling the regular expression, Tcl_RegExpCompile returns NULL and leaves an error message in the interpreter result. and listen β > the return value from Tcl_RegExpCompile is only valid up to the next call to Tcl_RegExpCompile; it is not safe to retain these values for long periods of time. do not cling to it, it's not that kind of token π
Tcl_RegExpExec actually runs the regular expression pattern matcher. it returns 1 if text contains a range of characters that match regexp, 0 if no match is found, and -1 if an error occurs. when there's an error, Tcl_RegExpExec leaves an error message in the interpreter result.
now, when you're searching a string for multiple matches of a pattern, it's super important to tell apart the start of the original string from the start of the current search. for example: when you go hunting for the second occurrence of a match, the text argument might point to the character right after the first match. but the pattern matcher needs to know that this isn't the start of the entire string, so that it doesn't let β^β atoms in the pattern match. that's literally what the start argument is for β it points to the start of the overall string containing text. start will be less than or equal to text, and if it is less than text then no ^ matches will be allowed. so set it right or your anchors will be acting up π
Tcl_RegExpRange can be invoked after Tcl_RegExpExec returns β it hands you the detailed tea about which ranges of the string matched which parts of the pattern β Tcl_RegExpRange returns a pair of pointers in *startPtr and *endPtr that identify a range of characters in the source string for the most recent call to Tcl_RegExpExec. index tells you which of several ranges you're getting: if index is 0, you get info about the overall range of characters that matched the entire pattern; otherwise you get info about the range of characters that matched the index'th parenthesized subexpression within the pattern. and if there's no range corresponding to index, then NULL is stored in *startPtr and *endPtr. simple as that β¨
π the value interfaces: Tcl_GetRegExpFromObj, Tcl_RegExpExecObj, Tcl_RegExpGetInfo π
Tcl_GetRegExpFromObj, Tcl_RegExpExecObj, and Tcl_RegExpGetInfo are value interfaces that give you the most direct control over Henry Spencer's regular expression library. if you're a user who needs to modify compilation and execution options directly, it is recommended that you use these interfaces instead of calling the internal regexp functions. they handle all the fiddly details of UTF to Unicode translations, plus they give you improved performance through caching in the pattern and string values. main character energy fr fr π₯
Tcl_GetRegExpFromObj tries to return a compiled regular expression from the patObj. if the value doesn't already contain a compiled regular expression, it will attempt to create one from the string in the value and assign it to the internal representation of the patObj. the return value of this function is of type Tcl_RegExp, and it's a token for this compiled form, which you can use in subsequent calls to Tcl_RegExpExecObj or Tcl_RegExpGetInfo. if an error occurs while compiling the regular expression, Tcl_GetRegExpFromObj returns NULL and leaves an error message in the interpreter result. the regular expression token can be used as long as the internal representation of patObj refers to the compiled form. π
the cflags argument is a bit-wise OR of zero or more of the following flags that control the compilation of patObj:
TCL_REG_ADVANCEDβ compile advanced regular expressions (βAREβs). this mode corresponds to the normal regular expression syntax accepted by the Tclregexpandregsubcommands. the fancy one πTCL_REG_EXTENDEDβ compile extended regular expressions (βEREβs). this mode corresponds to the regular expression syntax recognized by Tcl 8.0 and earlier versions. vintage, but make it fashion πΈTCL_REG_BASICβ compile basic regular expressions (βBREβs). this mode corresponds to the regular expression syntax recognized by common Unix utilities likesedandgrep. and guess what β this is the default if no flags are specified. the humble icon π
β¦and bestie, there's more where that came from β this page continues! don't go anywhere π
TCL_REG_EXPANDEDβ compiles your regex (basic, extended, or advanced, whichever vibe you picked) using an expanded syntax where comments and whitespace are allowed to just exist. In this mode, whitespace that isn't backslashed and isn't inside a bracket expression gets ignored, and so does any#-to-end-of-line comment. Living your truth, unbothered. β¨TCL_REG_QUOTEβ compiles a literal string, where every character is treated as an ordinary character. No special meanings, no drama, just text being text. πTCL_REG_NOCASEβ compiles for matching that ignores upper/lower case distinctions. Case-sensitive? Never heard of her. πTCL_REG_NEWLINEβ compiles for newline-sensitive matching. By default, newline is a completely ordinary character with no special meaning in either regular expressions or strings. With this flag on, though,β[^β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.REG_NEWLINEis the bit-wise OR ofREG_NLSTOPandREG_NLANCH. So it's giving both halves of the newline-sensitivity story. πTCL_REG_NLSTOPβ compiles for partial newline-sensitive matching, where the behavior ofβ[^βbracket expressions andβ.βis affected, but the behavior ofβ^βandβ$βis not. In this mode,β[^βbracket expressions andβ.βnever match newline. Half the vibe, all of the commitment. πTCL_REG_NLANCHβ compiles for inverse partial newline-sensitive matching, where the behavior ofβ^βandβ$β(the βanchorsβ) is affected, but the behavior ofβ[^βbracket expressions andβ.βis not. In this mode,β^β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. The other half of the vibe. π¦TCL_REG_NOSUBβ compiles for matching that reports only success or failure, not what was matched. This reduces compile overhead and may improve performance. Heads up though: subsequent calls toTcl_RegExpGetInfoorTcl_RegExpRangewill not report any match information. So you get a yes/no and that's it, bestie. πTCL_REG_CANMATCHβ compiles for matching that reports the potential to complete a partial match given more text (see below, iykyk).
Only one ofTCL_REG_EXTENDED,TCL_REG_ADVANCED,TCL_REG_BASIC, andTCL_REG_QUOTEmay be specified. Pick one, bestie, you can't have all four. β
Tcl_RegExpExecObj executes the regular expression pattern matcher. It returns 1 if objPtr contains a range of characters that match regexp, 0 if no match is found, and -1 if an error occurs. In the case of an error, Tcl_RegExpExecObj leaves an error message in the interpreter result. π
The nmatches value indicates to the matcher how many subexpressions are of interest. If nmatches is 0, then no subexpression match information is recorded, which may allow the matcher to make various optimizations. If the value is -1, then all of the subexpressions in the pattern are remembered. If the value is a positive integer, then only that number of subexpressions will be remembered.
Matching begins at the specified Unicode character index given by offset. Unlike Tcl_RegExpExec, the behavior of anchors is not affected by the offset value. Instead, the behavior of the anchors is explicitly controlled by the eflags argument, which is a bit-wise OR of zero or more of the following flags:
TCL_REG_NOTBOLβ the starting character will not be treated as the beginning of a line or the beginning of the string, soβ^βwill not match there. Note that this flag has no effect on howβ\Aβmatches. πTCL_REG_NOTEOLβ the last character in the string will not be treated as the end of a line or the end of the string, soβ$βwill not match there. Note that this flag has no effect on howβ\Zβmatches. π
Tcl_RegExpGetInfo retrieves information about the last match performed with a given regular expression regexp. The infoPtr argument contains a pointer to a structure that is defined as follows:
typedef struct {
Tcl_Size nsubs;
Tcl_RegExpIndices *matches;
Tcl_Size extendStart;
} Tcl_RegExpInfo;
The nsubs field contains a count of the number of parenthesized subexpressions within the regular expression. If the TCL_REG_NOSUB was used, then this value will be zero. The matches field points to an array of nsubs+1 values that indicate the bounds of each subexpression matched. The first element in the array refers to the range matched by the entire regular expression, and subsequent elements refer to the parenthesized subexpressions in the order that they appear in the pattern. Each element is a structure that is defined as follows:
typedef struct {
Tcl_Size start;
Tcl_Size end;
} Tcl_RegExpIndices;
The start and end values are Unicode character indices relative to the offset location within objPtr where matching began. The start index identifies the first character of the matched subexpression. The end index identifies the first character after the matched subexpression. If the subexpression matched the empty string, then start and end will be equal. If the subexpression did not participate in the match, then start and end will be set to -1. Not me getting emotional over empty-string matches π₯²
The extendStart field in Tcl_RegExpInfo is only set if the TCL_REG_CANMATCH flag was used. It indicates the first character in the string where a match could occur. If a match was found, this will be the same as the beginning of the current match. If no match was found, then it indicates the earliest point at which a match might occur if additional text is appended to the string. If it is no match is possible even with further text, this field will be set to -1. So basically: "here's where hope lives, or -1 if hope has left the chat." π«Ά
π REFERENCE COUNT MANAGEMENT π
Okay this part is important, do not skip it, this is the part where things crash if you're sloppy. β οΈ
The textObj and patObj arguments to Tcl_RegExpMatchObj must have reference counts of at least 1. Note however that this function may set the interpreter result; neither argument should be the direct interpreter result without an additional reference being taken. π
The patObj argument to Tcl_GetRegExpFromObj must have a reference count of at least 1. Note however that this function may set the interpreter result; the argument should not be the direct interpreter result without an additional reference being taken. π¦
The textObj argument to Tcl_RegExpExecObj must have a reference count of at least 1. Note however that this function may set the interpreter result; the argument should not be the direct interpreter result without an additional reference being taken. πΈ
π SEE ALSO π
π KEYWORDS π
match, pattern, regular expression, string, subexpression, Tcl_RegExpIndices, Tcl_RegExpInfo
And that's the whole page, bestie!! This one's from the Tcl crew, version 8.1, filed under Tcl_RegExpMatch(3tcl) β go forth and match responsibly, mwah πβ¨
π¦ 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) 1994 The Regents of the University of California
- Copyright (c) 1994-1996 Sun Microsystems, Inc
- Copyright (c) 1998-1999 Scriptics Corporation
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms