REGEXP(3)
π NAME π
okay so this is REGEXP(3) from the Library Functions Manual, and it's all about the regular expression lifestyle, bestie. The whole squad is here: regcomp, regcomplit, regcompnl, regexec, regsub, rregexec, rregsub, and regerror. Slay. π
β¨ SYNOPSIS β¨
#include <u.h>
#include <libc.h>
#include <regexp.h>
Reprog *regcomp(char *exp)
Reprog *regcomplit(char *exp)
Reprog *regcompnl(char *exp)
int regexec(Reprog *prog, char *string, Resub *match, int msize)
void regsub(char *source, char *dest, int dlen, Resub *match, int msize)
int rregexec(Reprog *prog, Rune *string, Resub *match, int msize)
void rregsub(Rune *source, Rune *dest, int dlen, Resub *match, int msize)
void regerror(char *msg)
π DESCRIPTION π
regcomp compiles a regular expression and returns a pointer to the generated description. The space is allocated by malloc(3) and may be released by free. Regular expressions are exactly as in regexp(7) β no cap.
regcomplit is like regcomp except that all characters are treated literally. regcompnl is like regcomp except that the . metacharacter matches all characters, including newlines. Obsessed with the range.
regexec matches a null-terminated string against the compiled regular expression in prog. If it matches, regexec returns 1 and fills in the array match with character pointers to the substrings of string that correspond to the parenthesized subexpressions of exp: match[i].sp points to the beginning and match[i].ep points just beyond the end of the ith substring. (Subexpression i begins at the *i*th left parenthesis, counting from 1.) Pointers in match[0] pick out the substring that corresponds to the whole regular expression. Unused elements of match are filled with zeros. The page literally says: βMatches involving and are extended as far as possible.β
yeah, there are blanks where some metacharacters should be in that sentence β Iβm not inventing them, bestie π
The number of array elements in match is given by msize. The structure of elements of match is:
typedef struct {
union {
char *sp;
Rune *rsp;
} s;
union {
char *ep;
Rune *rep;
} e;
} Resub;
If match[0].s.sp is nonzero on entry, regexec starts matching at that point within string. If match[0].e.ep is nonzero on entry, the last character matched is the one preceding that point.
regsub places in dest a substitution instance of source in the context of the last regexec performed using match. Each instance of \n, where n is a digit, is replaced by the string delimited by match[n].sp and match[n].ep. Each instance of [the original page literally has a blank here] is replaced by the string delimited by match[0].sp and match[0].ep.
another blank in the original, not me guessing π
The substitution will always be null terminated and trimmed to fit into dlen bytes.
regerror, called whenever an error is detected in regcomp, writes the string msg on the standard error file and exits. regerror can be replaced to perform special error processing. If the user supplied regerror returns rather than exits, regcomp will return 0.
rregexec and rregsub are variants of regexec and regsub that use strings of Runes instead of strings of chars. With these routines, the rsp and rep fields of the match array elements should be used.
πΈ SOURCE πΈ
/src/libregexp β that's the source tree, bestie.
π₯ SEE ALSO π₯
grep(1) β go say hi.
β DIAGNOSTICS β
regcomp returns 0 for an illegal expression or other failure. regexec returns 0 if string is not matched. Not the slay we wanted, but that's the diagnostic.
π BUGS π
There is no way to specify or match a NUL character; NULs terminate patterns and strings. So do not try to sneak a NUL in there β patterns and strings are done at NUL, periodt.
And that's the whole REGEXP(3) page from the Library Functions Manual, source tree /src/libregexp, and the footer is literally just REGEXP(3) β no date or version hanging out β so byeeee ππ¦
π¦ where this page comes from
- alpine 3.24: plan9port-doc 0_git20260208-r0 Β· LPL-1.02
- arch rolling: plan9port 20261021-2 Β· custom
- nixos 26.05: plan9port 0-unstable-2025-11-10
- nixos 26.05: plan9port-wayland 0-unstable-2025-11-26
- nixpkgs unstable: plan9port 0-unstable-2025-11-10
- nixpkgs unstable: plan9port-wayland 0-unstable-2025-11-26
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms