regex(3) Library Functions Manual regex(3) regcomp, regexec, regerror, regfree - POSIX (libc -lc) #include int regcomp(regex_t *restrict preg, const char *restrict regex, int cflags); int regexec(const regex_t *restrict preg, const char *restrict string, size_t n, regmatch_t pmatch[_Nullable restrict n], int eflags); size_t regerror(size_t errbuf_size; int errcode, const regex_t *_Nullable restrict preg, char errbuf[_Nullable restrict errbuf_size], size_t errbuf_size); void regfree(regex_t *preg); typedef struct { size_t re_nsub; } regex_t; typedef struct { regoff_t rm_so; regoff_t rm_eo; } regmatch_t; typedef /* ... */ regoff_t; regcomp() regexec() . *preg. regex . regexec(). regcomp() preg->re_nsub regex. preg->re_nsub + 1 n regexec() . cflags OR : REG_EXTENDED POSIX regex. POSIX. REG_ICASE . regexec() . REG_NOSUB . regexec() pmatch REG_STARTEND n. REG_NEWLINE . ([^...]) . (^) eflags regexec() REG_NOTBOL. ($) eflags REG_NOTEOL. regexec() *preg regcomp(). eflags OR : REG_NOTBOL ( REG_NEWLINE ). regexec() . REG_NOTEOL ( REG_NEWLINE ). REG_STARTEND [string + pmatch[0].rm_so, string + pmatch[0].rm_eo) [string, strnul(string)). NUL . ( REG_NOSUB regcomp() n > 0) pmatch string ( string + pmatch[0].rm_so). BSD POSIX. REG_NOSUB regcomp() string: regexec() n pmatch : pmatch[0] pmatch[1] . n pmatch -1. ( -1) [string + rm_so, string + rm_eo). regoff_t ptrdiff_t ssize_t. regerror() regcomp() regexec() . preg errcode preg. errbuf_size 0 errbuf_size errbuf . regfree() *preg *preg regcomp(). regcomp() . regexec() REG_NOMATCH . regerror() . regcomp(): REG_BADBR . REG_BADPAT . REG_BADRPT '*' . REG_EBRACE . REG_EBRACK . REG_ECOLLATE . REG_ECTYPE . REG_EEND . POSIX. REG_EESCAPE . REG_EPAREN . REG_ERANGE . REG_ESIZE 64 . POSIX. REG_ESPACE regex. REG_ESUBREG . attributes(7). +----------------------+------------------------------------+---------------------------------------------------------------------------------------------+ | | | | +----------------------+------------------------------------+---------------------------------------------------------------------------------------------+ |regcomp(), regexec() | | (locale) (MT-Safe) | +----------------------+------------------------------------+---------------------------------------------------------------------------------------------+ |regerror() | | (MT-Safe) | +----------------------+------------------------------------+---------------------------------------------------------------------------------------------+ |regfree() | | MT-Safe | +----------------------+------------------------------------+---------------------------------------------------------------------------------------------+ POSIX.1-2008. POSIX.1-2001. POSIX.1-2008 regoff_t off_t ssize_t. re_nsub REG_NOSUB . regex_t regmatch_t ( ) . . #include #include #include #include #include static const char *const str = "1) John Driverhacker;\n2) John Doe;\n3) John Foo;\n"; static const char *const re = "John.*o"; int main(void) { static const char *s = str; regex_t regex; regmatch_t pmatch[1]; regoff_t off, len; if (regcomp(®ex, re, REG_NEWLINE)) exit(EXIT_FAILURE); printf("String = \"%s\"\n", str); printf("Matches:\n"); for (unsigned int i = 0; ; i++) { if (regexec(®ex, s, countof(pmatch), pmatch, 0)) break; off = pmatch[0].rm_so + (s - str); len = pmatch[0].rm_eo - pmatch[0].rm_so; printf("#%u:\n", i); printf("offset = %jd; length = %jd\n", (intmax_t) off, (intmax_t) len); printf("substring = \"%.*s\"\n", len, s + pmatch[0].rm_so); s += pmatch[0].rm_eo; } exit(EXIT_SUCCESS); } grep(1), regex(7) glibc 3 . . : . 6.18 22 2026 regex(3)