Tcl_RegExpMatch(3)
π Tcl_RegExpMatch(3) β Tcl Library Procedures π
okay so bestie, welcome to the C side of regexp π
this is the Tcl_RegExpMatch page from the Tcl Library Procedures, and it's giving extremely useful. it's long tho, so we're doing this in parts β this is part 1. buckle up π
β¨ NAME β¨
Tcl_RegExpMatch, Tcl_RegExpCompile, Tcl_RegExpExec, Tcl_RegExpRange, Tcl_GetRegExpFromObj, Tcl_RegExpMatchObj, Tcl_RegExpExecObj, Tcl_RegExpGetInfo β pattern matching with regular expressions π₯
like, the whole squad. every one of them does regexp stuff, just at different levels of "how much do you want to touch the guts".
π¦ SYNOPSIS π¦
#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)
no cap, that's the whole squad assembled. β¨
π ARGUMENTS π
Tcl_Interp *interp(in) β the Tcl interpreter we use to report errors back to. and listen: this can beNULLif you genuinely do not want error reporting. main character choice, we respect it.Tcl_Obj *textObj(in/out) β points at the value we're pulling the text-to-search out of. heads up: the internal representation of that value may get converted into a form that can be searched efficiently. it's a glow-up, it's fine.Tcl_Obj *patObj(in/out) β points at the value we're getting a regular expression from. the compiled regexp gets cached in the value, so it's not re-doing the work every time. efficient queen πconst char *text(in) β the text we're searching for a match with a regular expression.const char *pattern(in) β a string in the form of a regular expression pattern.Tcl_RegExp regexp(in) β a compiled regular expression. it must have been returned previously byTcl_GetRegExpFromObjorTcl_RegExpCompile. no making these up yourself, bestie, they don't spawn from vibes.const char *start(in) β iftextis just a portion of some other string, this argument identifies the beginning of the larger string. if it is not the same astext, then no^matches will be allowed. so it's the "where did this string actually start" receipt π§ΎTcl_Size index(in) β specifies which range is desired: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 stored 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 stored here, orNULLif there is no such range. (yes, just after. classic C. we love it, we fear it. π)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. see below for more info β they're coming, don't worry.Tcl_Size offset(in) β the character offset into the text where matching should begin. the value of the offset has no impact on^matches. that behavior is controlled byeflags. important distinction, write it down πTcl_Size nmatches(in) β how many matching subexpressions should be remembered for later use. if this value is0, then no subexpression match information will be computed. if the value is negative, then all of the matching subexpressions will be remembered. any other value is taken as the maximum number of subexpressions to remember.int eflags(in) β an OR-ed combination of the execution flagsTCL_REG_NOTBOLandTCL_REG_NOTEOL. see below for more info.Tcl_RegExpInfo *infoPtr(out) β the address of the location where info about a previous match should be stored byTcl_RegExpGetInfo.
π DESCRIPTION π
Tcl_RegExpMatch determines whether its pattern argument matches regexp, where regexp is interpreted as a regular expression using the rules in the re_syntax reference page. so:
- if there is a match,
Tcl_RegExpMatchreturns1β - if there is no match, it returns
0π - if an error happens during the matching process (like, say,
patternis not a valid regular expression), it returns-1and leaves an error message in the interpreter result π
Tcl_RegExpMatchObj is basically Tcl_RegExpMatch's cooler sibling β same energy, except it operates on the Tcl values textObj and patObj instead of UTF strings. and here's the tea: Tcl_RegExpMatchObj is generally more efficient than Tcl_RegExpMatch, so it's the preferred interface. periodt. π
Tcl_RegExpCompile, Tcl_RegExpExec, and Tcl_RegExpRange give you lower-level access to the regular expression pattern matcher β like backstage passes ποΈ
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 subsequent calls to Tcl_RegExpExec or Tcl_RegExpRange. if an error occurs while compiling the regular expression, Tcl_RegExpCompile returns NULL and leaves an error message in the interpreter result.
β οΈ big warning bestie: the return value fromTcl_RegExpCompileis only valid up to the next call toTcl_RegExpCompile. it is not safe to retain these values for long periods of time. do not be clingy with this token, it will not love you back π
Tcl_RegExpExec executes the regular expression pattern matcher. it returns:
1iftextcontains a range of characters that matchregexp0if no match is found-1if an error occurs
and if there's an error, Tcl_RegExpExec leaves an error message in the interpreter result.
now this next part is the one everybody gets wrong, so listen up π«Ά when you're searching a string for multiple matches of a pattern, it's important to distinguish between the start of the original string and the start of the current search. example: when searching for the second occurrence of a match, the text argument might point to the character just after the first match. but the pattern matcher needs to know that this is not the start of the entire string β otherwise it'd let ^ atoms in the pattern match when they absolutely should not. the start argument provides exactly that info by pointing to the start of the overall string containing text. start will be less than or equal to text; if it is less than text, then no ^ matches will be allowed. iconic behavior honestly β¨
Tcl_RegExpRange may be invoked after Tcl_RegExpExec returns, and it spills the details about what ranges of the string matched what parts of the pattern π it 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 says which of several ranges you want:
- if
indexis0, 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 is no range corresponding to
index, thenNULLis stored in*startPtrand*endPtr. no range, no problem πΈ
Tcl_GetRegExpFromObj, Tcl_RegExpExecObj, and Tcl_RegExpGetInfo are value interfaces that provide the most direct control of Henry Spencer's regular expression library. (yes, that Henry Spencer. legend. π) if you're a user who needs to modify compilation and execution options directly, the recommendation is: use these interfaces instead of calling the internal regexp functions. they handle all the fiddly details of UTF to Unicode translations, and they give you improved performance through caching in the pattern and string values. ate. π
Tcl_GetRegExpFromObj attempts to return a compiled regular expression from the patObj. if the value does not 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. that return value is a token for this compiled form, which can be used 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. (so, again β don't hoard it, bestie, its lifespan is tied to that object π¦)
the cflags argument is a bit-wise OR of zero or more of the following flags that control the compilation of patObj:
π compilation flags π
TCL_REG_ADVANCEDβ Compile advanced regular expressions (βAREβs). This mode corresponds to the normal regular expression syntax accepted by the Tclregexpandregsubcommands. this is the one you probably want, fr fr.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. a little vintage moment πTCL_REG_BASICβ Compile basic regular expressions (βBREβs). This mode corresponds to the regular expression syntax recognized by common Unix utilities likesedandgrep. This is the default if no flags are specified. so if you pass nothing, this is what you get. surprise! (not really, it's literally in the docs. but still.) π
and that's not even all the flags, bestie β we're only getting started. part 2 incoming π
TCL_REG_EXPANDEDβ okay so this one compiles the regular expression (basic, extended, or advanced) using an expanded syntax that lets you have comments and whitespace. It makes non-backslashed, non-bracket-expression white space and#-to-end-of-line comments get ignored. πTCL_REG_QUOTEβ compiles a literal string, fr fr, with all characters treated as ordinary characters. β¨TCL_REG_NOCASEβ compiles for matching that ignores upper/lower case distinctions. soAandaare literally the same. π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,[^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. πTCL_REG_NLSTOPβ compiles for partial newline-sensitive matching, with the behavior of[^bracket expressions and.affected, but not the behavior of^and$. in this mode,[^bracket expressions and.never match newline. πΈTCL_REG_NLANCHβ compiles for inverse partial newline-sensitive matching, with the behavior of^and$(the βanchorsβ) affected, but not the behavior of[^bracket expressions and.. 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. π¦TCL_REG_NOSUBβ compiles for matching that reports only success or failure, not what was matched. this reduces compile overhead and may improve performance. subsequent calls toTcl_RegExpGetInfoorTcl_RegExpRangewill not report any match information. πTCL_REG_CANMATCHβ compiles for matching that reports the potential to complete a partial match given more text (see below). π
only one ofTCL_REG_EXTENDED,TCL_REG_ADVANCED,TCL_REG_BASIC, andTCL_REG_QUOTEmay be specified. periodt. π
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 tells 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\Amatches. π«Ά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\Zmatches. π
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.
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 no match is possible even with further text, this field will be set to -1. π
β¨ REFERENCE COUNT MANAGEMENT β¨
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
π okay bestie, that's the whole page! it's from Tcl 8.1 and it's the Tcl_RegExpMatch(3) manual. you're literally a regex queen now. slay! π
π¦ 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) 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