regexのヘルプ・マニュアル
日本語 英語
regex --help
man regex
REGEX(3) Linux Programmer’s Manual REGEX(3)
名前
regcomp, regexec, regerror, regfree - POSIX regex 関数
書式
#include
#include
int regcomp(regex_t *preg, const char *regex, int cflags);
int regexec(const regex_t *preg, const char *string, size_t nmatch,
regmatch_t pmatch[], int eflags);
size_t regerror(int errcode, const regex_t *preg, char *errbuf,
size_t errbuf_size);
void regfree(regex_t *preg);
説明
POSIX regex コンパイル
regcomp() は、正規表現をコンパイルして、 regexec() での検索処理に適合す
る形態にする。
regcomp() はパターンを記憶するバッファへのポインタ preg、ヌル文字で終端
された文字列 regex、そしてコンパイルの形式を決めるためのフラグ cflag を
引数に伴う。
全ての正規表現検索は、コンパイルされたパターンによって行わなければな ら
ない。よって、 regexec() に指定するのは、必ず (regcomp() によってコンパ
イルされた) パターンバッファへのアドレスでなければならない。
cflags には以下に示す定数一つ以上のビットごとの OR (bitwise-or) を指 定
する。
REG_EXTENDED
regex に POSIX 拡張正規表現を使用する。もしこのフラグが設定され
ない場合、 POSIX 標準正規表現が使われる。
REG_ICASE
大文字小文字の違いを無視する。このフラグを指定してコンパイルされ
た パターンバッファを用いて regexec() 関数を呼び出すと、大文字小
文字の区別を付けずに検索が行われる。
REG_NOSUB
このフラグを設定してコンパイルされたパターンバッファが regexec()
の引数に指定されると、引き数 nmatch, pmatch が無視される。
REG_NEWLINE
全ての文字にマッチするオペレータに改行をマッチさせない。
改 行を含まない非マッチング文字リスト ([^...]) に改行をマッチさ
せない。
regexec() の実行時に指定するフラグ eflags に REG_NOTBOL を含むか
ど うかにかかわらず、行頭にマッチするオペレータ (^) を改行直後の
空文字列にマッチさせる。
eflags に REG_NOTEOL を含むかどうかにかかわらず、行末にマッチ す
るオペレータ ($) を改行直前の空文字列にマッチさせる。
POSIX regex マッチング
regexec() は、プリコンパイルされたパターンバッファ preg をヌル文字で終
端された文字列にマッチさせる。 nmatch と pmatch はマッチングの位置に 関
する情報を取得するのに用いられる。 eflags には REG_NOTBOL と REG_NOTEOL
のどちらか、もしくは両方のビットごとの OR (bitwise-or) を指定し、以下で
説明するようにマッチング動作を変化させる。
REG_NOTBOL
行頭にマッチするオペレータは、必ずマッチに失敗する (コンパイル時
のフラグ REG_NEWLINE の項目も参照)。このフラグは、複数行にまたが
る 文字列を regexec() で検索する際に、文字列の先頭を行の先頭とし
て解釈させない場合に用いる。
REG_NOTEOL
行末にマッチするオペレータは、必ずマッチに失敗する (コンパイル時
のフラグ REG_NEWLINE の項目も参照)。
バイトオフセット
パターンバッファのコンパイル時に REG_NOSUB が設定されない場合は、部分文
字列のマッチング位置情報を得ることができる。 pmatch は、 少 な く と も
nmatch の大きさを持つように指定しなければならない。 regexec() の実行に
よって、それらに部分文字列マッチング位置情報が代入される。未使用の構 造
体要素には -1 が値として代入される。
pmatch の型である regmatch_t 構造体は、 内で定義される。
typedef struct {
regoff_t rm_so;
regoff_t rm_eo;
} regmatch_t;
構造体要素 rm_so の値が -1 でない場合、それは文字列内での次の最大のマッ
チング部分の開始オフセット位置を示す。それに対し、構造体要素 rm_eo はマ
ッ チング部分の終了オフセット位置を示し、マッチング部分の直後の文字のオ
フセット位置が使用される。
POSIX エラーレポート
regerror() は、 regcomp() と regexec() の実行によって得られるエラーコー
ドから、エラーメッセージ文字列を得るのに用いられる。
regerror() はエラーコード errcode、パターンバッファ preg、文字列バッフ
ァへのポインタ errbuf、文字列バッファのサイズ errbuf_size を引数にと る
。 この関数は、ヌル文字で終端されたエラーメッセージ文字列を格納するのに
必要な errbuf のサイズを返す。もし errbuf と errbuf_size の両方が 非 0
値であれば、 errbuf には最初の errbuf_size - 1 文字分にエラーメッセージ
と終端のヌル文字が収まるように代入される。
POSIX パターンバッファ解放
引数にコンパイルされたパターンバッファ preg を与えて regfree() を呼び出
すと、 regcomp() によるコンパイル時にパターンバッファに割り当てられたメ
モリが解放される。
返り値
regcomp() は、コンパイルの成功時には 0 を返し、失敗時にはエラーコードを
返す。
regexec() は、マッチングの成功時には 0 を返し、失敗時には REG_NOMATCH
を返す。
エラー
regcomp() は以下のエラーを返す。
REG_BADBR
無効な後方参照オペレータの使用。
REG_BADPAT
グループやリストなどの、パターンオペレータの無効な使用。
REG_BADRPT
'*' が最初の文字としてくるような、無効な繰り返しオペレータの使用
。
REG_EBRACE
イ ンターバルオペレータ {} (brace interval operators) が閉じてい
ない。
REG_EBRACK
リストオペレータ [] (bracket list operators) が閉じていない。
REG_ECOLLATE
照合順序の要素 (collating element) として有効ではない。
(訳注) 照合順序の要素 (collating element) については、 regex(7)
を参照。
REG_ECTYPE
未知のキャラクタクラス名。
REG_EEND
未定義エラー。これは POSIX.2 には定義されていない。
REG_EESCAPE
正規表現がバックスラッシュで終っている。
( 訳注) 日本語環境の場合、バックスラッシュとなるべき所が円記号で
表示されることがあるが、これは単に表示フォントの問題で、内部的に
は同じ意味である。
REG_EPAREN
グループオペレータ () (parenthesis group operators) が閉じていな
い。
REG_ERANGE
無効な範囲オペレータの使用。例えば、範囲の終了位置が開始位置より
も前にあるような場合。
REG_ESIZE
正 規表現のコンパイルに、64Kb 以上のパターンバッファが必要。これ
は POSIX.2 には定義されていない。
REG_ESPACE
regex ルーチンがメモリを使いはたしている。
REG_ESUBREG
サブエクスプレッション \(...\) (subexpression) への無効な後方 参
照。
準拠
POSIX.1-2001.
関連項目
grep(1), regex(7), GNU regex マニュアル
GNU 2008-05-29 REGEX(3)
REGEX(3) Linux Programmer’s Manual REGEX(3)
NAME
regcomp, regexec, regerror, regfree - POSIX regex functions
SYNOPSIS
#include
#include
int regcomp(regex_t *preg, const char *regex, int cflags);
int regexec(const regex_t *preg, const char *string, size_t nmatch,
regmatch_t pmatch[], int eflags);
size_t regerror(int errcode, const regex_t *preg, char *errbuf,
size_t errbuf_size);
void regfree(regex_t *preg);
DESCRIPTION
POSIX Regex Compiling
regcomp() is used to compile a regular expression into a form that is
suitable for subsequent regexec() searches.
regcomp() is supplied with preg, a pointer to a pattern buffer storage
area; regex, a pointer to the null-terminated string and cflags, flags
used to determine the type of compilation.
All regular expression searching must be done via a compiled pattern
buffer, thus regexec() must always be supplied with the address of a
regcomp() initialized pattern buffer.
cflags may be the bitwise-or of one or more of the following:
REG_EXTENDED
Use POSIX Extended Regular Expression syntax when interpreting
regex. If not set, POSIX Basic Regular Expression syntax is
used.
REG_ICASE
Do not differentiate case. Subsequent regexec() searches using
this pattern buffer will be case insensitive.
REG_NOSUB
Support for substring addressing of matches is not required.
The nmatch and pmatch arguments to regexec() are ignored if the
pattern buffer supplied was compiled with this flag set.
REG_NEWLINE
Match-any-character operators don’t match a newline.
A non-matching list ([^...]) not containing a newline does not
match a newline.
Match-beginning-of-line operator (^) matches the empty string
immediately after a newline, regardless of whether eflags, the
execution flags of regexec(), contains REG_NOTBOL.
Match-end-of-line operator ($) matches the empty string immedi-
ately before a newline, regardless of whether eflags contains
REG_NOTEOL.
POSIX Regex Matching
regexec() is used to match a null-terminated string against the precom-
piled pattern buffer, preg. nmatch and pmatch are used to provide
information regarding the location of any matches. eflags may be the
bitwise-or of one or both of REG_NOTBOL and REG_NOTEOL which cause
changes in matching behavior described below.
REG_NOTBOL
The match-beginning-of-line operator always fails to match (but
see the compilation flag REG_NEWLINE above) This flag may be
used when different portions of a string are passed to regexec()
and the beginning of the string should not be interpreted as the
beginning of the line.
REG_NOTEOL
The match-end-of-line operator always fails to match (but see
the compilation flag REG_NEWLINE above)
Byte Offsets
Unless REG_NOSUB was set for the compilation of the pattern buffer, it
is possible to obtain substring match addressing information. pmatch
must be dimensioned to have at least nmatch elements. These are filled
in by regexec() with substring match addresses. Any unused structure
elements will contain the value -1.
The regmatch_t structure which is the type of pmatch is defined in
.
typedef struct {
regoff_t rm_so;
regoff_t rm_eo;
} regmatch_t;
Each rm_so element that is not -1 indicates the start offset of the
next largest substring match within the string. The relative rm_eo
element indicates the end offset of the match, which is the offset of
the first character after the matching text.
Posix Error Reporting
regerror() is used to turn the error codes that can be returned by both
regcomp() and regexec() into error message strings.
regerror() is passed the error code, errcode, the pattern buffer, preg,
a pointer to a character string buffer, errbuf, and the size of the
string buffer, errbuf_size. It returns the size of the errbuf required
to contain the null-terminated error message string. If both errbuf
and errbuf_size are non-zero, errbuf is filled in with the first
errbuf_size - 1 characters of the error message and a terminating null.
POSIX Pattern Buffer Freeing
Supplying regfree() with a precompiled pattern buffer, preg will free
the memory allocated to the pattern buffer by the compiling process,
regcomp().
RETURN VALUE
regcomp() returns zero for a successful compilation or an error code
for failure.
regexec() returns zero for a successful match or REG_NOMATCH for fail-
ure.
ERRORS
The following errors can be returned by regcomp():
REG_BADBR
Invalid use of back reference operator.
REG_BADPAT
Invalid use of pattern operators such as group or list.
REG_BADRPT
Invalid use of repetition operators such as using '*' as the
first character.
REG_EBRACE
Un-matched brace interval operators.
REG_EBRACK
Un-matched bracket list operators.
REG_ECOLLATE
Invalid collating element.
REG_ECTYPE
Unknown character class name.
REG_EEND
Non specific error. This is not defined by POSIX.2.
REG_EESCAPE
Trailing backslash.
REG_EPAREN
Un-matched parenthesis group operators.
REG_ERANGE
Invalid use of the range operator, e.g., the ending point of the
range occurs prior to the starting point.
REG_ESIZE
Compiled regular expression requires a pattern buffer larger
than 64Kb. This is not defined by POSIX.2.
REG_ESPACE
The regex routines ran out of memory.
REG_ESUBREG
Invalid back reference to a subexpression.
CONFORMING TO
POSIX.1-2001.
SEE ALSO
grep(1), regex(7), GNU regex manual
COLOPHON
This page is part of release 3.22 of the Linux man-pages project. A
description of the project, and information about reporting bugs, can
be found at http://www.kernel.org/doc/man-pages/.
GNU 2008-05-29 REGEX(3)