regex
Section: C Library Functions (3)
Updated: 2026-02-22
Index
JM Home Page
roff page
名前
regcomp, regexec, regerror, regfree - POSIX regex 関数
ライブラリ
標準 C ライブラリ (libc,~-lc)
書式
#include <regex.h>
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;
説明
Compilation
regcomp() は、正規表現をコンパイルして、 regexec() での検索処理に適合する形態にする。
On success, the pattern buffer at *preg is initialized. regex is a
null-terminated string. The locale must be the same when running
regexec().
After regcomp() succeeds, preg->re_nsub holds the number of
subexpressions in regex. Thus, a value of preg->re_nsub + 1
passed as n to regexec() is sufficient to capture all matches.
cflags には、以下に示す定数のうち 0 個以上をビットごとの OR (bitwise-or) で指定する。
- REG_EXTENDED
-
regex に POSIX 拡張正規表現を使用する。もしこのフラグが設定されない場合、 POSIX 標準正規表現が使われる。
- REG_ICASE
-
大文字小文字の違いを無視する。このフラグを指定してコンパイルされた パターンバッファーを用いて regexec()
関数を呼び出すと、大文字小文字の区別を付けずに検索が行われる。
- REG_NOSUB
-
Report only overall success. regexec() will use only pmatch for
REG_STARTEND, ignoring n.
- REG_NEWLINE
-
全ての文字にマッチするオペレータに改行をマッチさせない。
-
A nonmatching list ([^...]) not containing a newline does not
match a newline.
-
regexec() の実行時に指定するフラグ eflags に REG_NOTBOL
を含むかどうかにかかわらず、行頭にマッチするオペレータ (^) を改行直後の空文字列にマッチさせる。
-
eflags に REG_NOTEOL を含むかどうかにかかわらず、行末にマッチするオペレータ ($)
を改行直前の空文字列にマッチさせる。
Matching
regexec() is used to match a null-terminated string against the compiled
pattern buffer in *preg, which must have been initialised with
regcomp(). eflags is the bitwise OR of zero or more of the following
flags:
- REG_NOTBOL
-
行頭にマッチするオペレータは、必ずマッチに失敗する (コンパイル時のフラグ REG_NEWLINE の項目も参照)。
このフラグは、複数行にまたがる文字列を regexec() で検索する際に、文字列の先頭を行の先頭として解釈させない場合に用いる。
- REG_NOTEOL
-
行末にマッチするオペレータは、必ずマッチに失敗する (コンパイル時のフラグ REG_NEWLINE の項目も参照)。
- REG_STARTEND
-
Match [string + pmatch[0].rm_so, string + pmatch[0].rm_eo) instead of
[string,~strnul(string)). This allows matching embedded NUL bytes.
If any matches are returned (REG_NOSUB wasn't passed to regcomp(), the
match succeeded, and n > 0), they overwrite pmatch as usual, and
the match offsets remain relative to string (not string + pmatch[0].rm_so). This flag is a BSD extension, not present in POSIX.
Match offsets
Unless REG_NOSUB was passed to regcomp(), it is possible to obtain the
locations of matches within string: regexec() fills n elements of
pmatch with results: pmatch[0] corresponds to the entire match,
pmatch[1] to the first subexpression, etc. If there were more matches
than n, they are discarded; if fewer, unused elements of pmatch are
filled with -1s.
Each returned valid (non--1) match corresponds to the range [string + rm_so, string + rm_eo).
regoff_t is a signed integer type capable of storing the largest value
that can be stored in either an ptrdiff_t type or a ssize_t type.
Error reporting
regerror() は、 regcomp() と regexec()
の実行によって得られるエラーコードから、エラーメッセージ文字列を 得るのに用いられる。
If preg isn't a null pointer, errcode must be the latest error
returned from an operation on preg.
If errbuf_size isn't 0, up to errbuf_size bytes are copied to
errbuf; the error string is always null-terminated, and truncated to fit.
Freeing
regfree() deinitializes the pattern buffer at *preg, freeing any
associated memory; *preg must have been initialized via regcomp().
返り値
regcomp() は、コンパイルの成功時には 0 を返し、失敗時にはエラーコードを返す。
regexec() は、マッチングの成功時には 0 を返し、失敗時には REG_NOMATCH を返す。
regerror() returns the size of the buffer required to hold the string.
エラー
regcomp() は以下のエラーを返す。
- REG_BADBR
-
無効な後方参照オペレータの使用。
- REG_BADPAT
-
グループやリストなどの、パターンオペレータの無効な使用。
- REG_BADRPT
-
Invalid use of repetition operators such as using '*' as the first
character.
- REG_EBRACE
-
インターバルオペレータ {} (brace interval operators) が閉じていない。
- REG_EBRACK
-
リストオペレータ [] (bracket list operators) が閉じていない。
- REG_ECOLLATE
-
照合順序の要素 (collating element) として有効ではない。 (訳注) 詳細は regex(7) を参照。
- REG_ECTYPE
-
未知のキャラクタークラス名。
- REG_EEND
-
未定義エラー。これは POSIX には定義されていない。
- REG_EESCAPE
-
正規表現がバックスラッシュで終っている。
- REG_EPAREN
-
グループオペレータ () (parenthesis group operators) が閉じていない。
- REG_ERANGE
-
無効な範囲オペレータの使用。 例えば、範囲の終了位置が開始位置よりも前にあるような場合。
- REG_ESIZE
-
正規表現のコンパイルに、64 Kb 以上のパターンバッファーが必要。 これは POSIX には定義されていない。
- REG_ESPACE
-
regex ルーチンがメモリーを使いはたしている。
- REG_ESUBREG
-
サブエクスプレッション \(...\) (subexpression) への無効な後方参照。
属性
この節で使用されている用語の説明については、 attributes(7) を参照。
| インターフェース | 属性 | 値
|
|
regcomp(),
regexec()
| Thread safety | MT-Safe locale
|
|
regerror()
| Thread safety | MT-Safe env
|
|
regfree()
| Thread safety | MT-Safe
|
標準規格
POSIX.1-2008.
歴史
POSIX.1-2001.
Prior to POSIX.1-2008, regoff_t was required to be capable of storing the
largest value that can be stored in either an off_t type or a ssize_t
type.
警告
re_nsub is only required to be initialized if REG_NOSUB wasn't
specified, but all known implementations initialize it regardless.
Both regex_t and regmatch_t may (and do) have more members, in any
order. Always reference them by name.
例
#include <stdcountof.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <regex.h>
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 マニュアルのセクション Regular Expressions
Index
- 名前
-
- ライブラリ
-
- 書式
-
- 説明
-
- Compilation
-
- Matching
-
- Match offsets
-
- Error reporting
-
- Freeing
-
- 返り値
-
- エラー
-
- 属性
-
- 標準規格
-
- 歴史
-
- 警告
-
- 例
-
- 関連項目
-
This document was created by
man2html,
using the manual pages.