Tcl_RegExpMatch(3)

πŸ“¦
version 9.0.3 Β· 3 versions
, in alpine 3.23, alpine 3.24, fedora 43, fedora 44, nixos 26.05, nixpkgs unstable

πŸ’– 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 be NULL if 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 by Tcl_GetRegExpFromObj or Tcl_RegExpCompile. no making these up yourself, bestie, they don't spawn from vibes.
  • const char *start (in) β€” if text is just a portion of some other string, this argument identifies the beginning of the larger string. if it is not the same as text, 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: 0 means the range of the entire match, 1 or 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, or NULL if 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, or NULL if 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 flags TCL_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, and TCL_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 by eflags. important distinction, write it down πŸ“
  • Tcl_Size nmatches (in) β€” how many matching subexpressions should be remembered for later use. if this value is 0, 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 flags TCL_REG_NOTBOL and TCL_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 by Tcl_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_RegExpMatch returns 1 βœ…
  • if there is no match, it returns 0 πŸ™…
  • if an error happens during the matching process (like, say, pattern is not a valid regular expression), it returns -1 and 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 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 be clingy with this token, it will not love you back πŸ’€

Tcl_RegExpExec executes the regular expression pattern matcher. it returns:

  • 1 if text contains a range of characters that match regexp
  • 0 if no match is found
  • -1 if 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 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 is no range corresponding to index, then NULL is stored in *startPtr and *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 Tcl regexp and regsub commands. 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 like sed and grep. 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. so A and a are 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_NEWLINE is the bit-wise OR of REG_NLSTOP and REG_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 to Tcl_RegExpGetInfo or Tcl_RegExpRange will 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 of TCL_REG_EXTENDED, TCL_REG_ADVANCED, TCL_REG_BASIC, and TCL_REG_QUOTE may 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 \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.

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