1 .\" Copyright (C), 1995, Graeme W. Wilford. (Wilf.)
3 .\" %%%LICENSE_START(VERBATIM)
4 .\" Permission is granted to make and distribute verbatim copies of this
5 .\" manual provided the copyright notice and this permission notice are
6 .\" preserved on all copies.
8 .\" Permission is granted to copy and distribute modified versions of this
9 .\" manual under the conditions for verbatim copying, provided that the
10 .\" entire resulting derived work is distributed under the terms of a
11 .\" permission notice identical to this one.
13 .\" Since the Linux kernel and libraries are constantly changing, this
14 .\" manual page may be incorrect or out-of-date. The author(s) assume no
15 .\" responsibility for errors or omissions, or for damages resulting from
16 .\" the use of the information contained herein. The author(s) may not
17 .\" have taken the same level of care in the production of this manual,
18 .\" which is licensed free of charge, as they might when working
21 .\" Formatted or processed versions of this manual, if unaccompanied by
22 .\" the source, must acknowledge the copyright and authors of this work.
25 .\" Wed Jun 14 16:10:28 BST 1995 Wilf. (G.Wilford@ee.surrey.ac.uk)
26 .\" Tiny change in formatting - aeb, 950812
27 .\" Modified 8 May 1998 by Joseph S. Myers (jsm28@cam.ac.uk)
29 .\" show the synopsis section nicely
30 .\"*******************************************************************
32 .\" This file was generated with po4a. Translate the source file.
34 .\"*******************************************************************
36 .\" Japanese Version Copyright (c) 1998 Takatsugu Nokubi
37 .\" all rights reserved.
38 .\" Translated 1998-06-30, Takatsugu Nokubi
39 .\" Updated & Modified 2001-11-05, Yuichi SATO <ysato@h4.dion.ne.jp>
40 .\" Updated & Modified 2005-02-24, Yuichi SATO <ysato444@yahoo.co.jp>
41 .\" Updated 2008-08-02, Akihiro MOTOKI <amotoki@dd.iij4u.or.jp>, LDP v3.05
42 .\" Updated 2013-03-27, Akihiro MOTOKI <amotoki@gmail.com>
44 .TH REGEX 3 2020\-08\-13 GNU "Linux Programmer's Manual"
46 regcomp, regexec, regerror, regfree \- POSIX regex 関数
49 \fB#include <regex.h>\fP
51 \fBint regcomp(regex_t *\fP\fIpreg\fP\fB, const char *\fP\fIregex\fP\fB, int \fP\fIcflags\fP\fB);\fP
53 \fBint regexec(const regex_t *\fP\fIpreg\fP\fB, const char *\fP\fIstring\fP\fB, size_t \fP\fInmatch\fP\fB,\fP
54 \fB regmatch_t \fP\fIpmatch[]\fP\fB, int \fP\fIeflags\fP\fB);\fP
56 \fBsize_t regerror(int \fP\fIerrcode\fP\fB, const regex_t *\fP\fIpreg\fP\fB, char *\fP\fIerrbuf\fP\fB,\fP
57 \fB size_t \fP\fIerrbuf_size\fP\fB);\fP
59 \fBvoid regfree(regex_t *\fP\fIpreg\fP\fB);\fP
62 .SS "POSIX regex コンパイル"
63 \fBregcomp\fP() は、正規表現をコンパイルして、 \fBregexec\fP() での検索処理に適合する形態にする。
65 \fBregcomp\fP() はパターンを記憶するバッファーへのポインター \fIpreg\fP、 ヌル文字で終端された文字列 \fIregex\fP、
66 そしてコンパイルの形式を決めるためのフラグ \fIcflag\fP を引数に伴う。
68 全ての正規表現検索は、コンパイルされたパターンによって行わなければならない。 よって、 \fBregexec\fP() に指定するのは、必ず
69 (\fBregcomp\fP() によってコンパイルされた) パターンバッファーへのアドレスでなければならない。
71 \fIcflags\fP には、以下に示す定数のうち 0 個以上をビットごとの OR (bitwise\-or) で指定する。
74 \fIregex\fP に \fBPOSIX\fP 拡張正規表現を使用する。もしこのフラグが設定されない場合、 \fBPOSIX\fP 標準正規表現が使われる。
77 大文字小文字の違いを無視する。このフラグを指定してコンパイルされた パターンバッファーを用いて \fBregexec\fP()
78 関数を呼び出すと、大文字小文字の区別を付けずに検索が行われる。
81 マッチの場所を報告しない。渡されたパターンバッファーがこのフラグを設定してコンパイルされていた場合、 \fBregexec\fP() の引き数
82 \fInmatch\fP, \fIpmatch\fP が無視される。
85 全ての文字にマッチするオペレータに改行をマッチさせない。
87 改行を含まない非マッチング文字リスト (\fB[\(ha...]\fP) に改行をマッチさせない。
89 \fBregexec\fP() の実行時に指定するフラグ \fIeflags\fP に \fBREG_NOTBOL\fP
90 を含むかどうかにかかわらず、行頭にマッチするオペレータ (\fB\(ha\fP) を改行直後の空文字列にマッチさせる。
92 \fIeflags\fP に \fBREG_NOTEOL\fP を含むかどうかにかかわらず、行末にマッチするオペレータ (\fB$\fP)
94 .SS "POSIX regex マッチング"
95 \fBregexec\fP() は、 プリコンパイルされたパターンバッファー \fIpreg\fP をヌル文字で終端された文字列にマッチさせる。
96 \fInmatch\fP と \fIpmatch\fP はマッチングの位置に関する情報を取得するのに用いられる。 \fIeflags\fP には、以下のフラグのうち 0
97 個以上をビットごとの \fBOR\fP (bitwise\-\fBor\fP) で指定する。
100 行頭にマッチするオペレータは、必ずマッチに失敗する (コンパイル時のフラグ \fBREG_NEWLINE\fP の項目も参照)。
101 このフラグは、複数行にまたがる文字列を \fBregexec\fP() で検索する際に、文字列の先頭を行の先頭として解釈させない場合に用いる。
104 行末にマッチするオペレータは、必ずマッチに失敗する (コンパイル時のフラグ \fBREG_NEWLINE\fP の項目も参照)。
107 Use \fIpmatch[0]\fP on the input string, starting at byte \fIpmatch[0].rm_so\fP
108 and ending before byte \fIpmatch[0].rm_eo\fP. This allows matching embedded
109 NUL bytes and avoids a \fBstrlen\fP(3) on large strings. It does not use
110 \fInmatch\fP on input, and does not change \fBREG_NOTBOL\fP or \fBREG_NEWLINE\fP
111 processing. This flag is a BSD extension, not present in POSIX.
113 パターンバッファーのコンパイル時に \fBREG_NOSUB\fP が設定されない場合は、マッチング位置情報を得ることができる。 \fIpmatch\fP
114 は、少なくとも \fInmatch\fP の大きさを持つように指定しなければならない。 \fBregexec\fP()
115 の実行によって、それらに部分文字列マッチング位置情報が代入される。 \fIi\fP 番目の括弧で始まる部分正規表現のオフセットは \fIpmatch[i]\fP
116 に格納される。正規表現全体のマッチアドレスは \fIpmatch[0]\fP に格納される。 (\fIN\fP 個の部分正規表現のマッチのオフセットを返すためには、
117 \fInmatch\fP は最低限 \fIN+1\fP でなければならない点に注意すること。) 未使用の構造体要素には \-1 が値として代入される。
119 \fIpmatch\fP の型である \fIregmatch_t\fP 構造体は、 \fI<regex.h>\fP 内で定義される。
130 構造体要素 \fIrm_so\fP の値が \-1 でない場合、それは文字列内での次の最大のマッチング部分の開始 オフセット位置を示す。それに対し、構造体要素
131 \fIrm_eo\fP はマッチング部分の終了オフセット位置を示し、 マッチング部分の直後の文字のオフセット位置が使用される。
133 \fBregerror\fP() は、 \fBregcomp\fP() と \fBregexec\fP()
134 の実行によって得られるエラーコードから、エラーメッセージ文字列を 得るのに用いられる。
136 \fBregerror\fP() はエラーコード \fIerrcode\fP、 パターンバッファー \fIpreg\fP、 文字列バッファーへのポインター
137 \fIerrbuf\fP、 文字列バッファーのサイズ \fIerrbuf_size\fP を引数にとる。
138 この関数は、ヌル文字で終端されたエラーメッセージ文字列を格納するのに必要な \fIerrbuf\fP のサイズを返す。もし \fIerrbuf\fP と
139 \fIerrbuf_size\fP の両方が非 0 値であれば、 \fIerrbuf\fP には最初の \fIerrbuf_size \- 1\fP
140 文字分にエラーメッセージと終端のヌルバイト (\(aq\e0\(aq) が収まるように代入される。
141 .SS "POSIX パターンバッファー解放"
142 引数にコンパイルされたパターンバッファー \fIpreg\fP を与えて \fBregfree\fP() を呼び出すと、 \fBregcomp\fP()
143 によるコンパイル時にパターンバッファーに割り当てられたメモリーが解放される。
145 \fBregcomp\fP() は、コンパイルの成功時には 0 を返し、失敗時にはエラーコードを返す。
147 \fBregexec\fP() は、マッチングの成功時には 0 を返し、失敗時には \fBREG_NOMATCH\fP を返す。
149 \fBregcomp\fP() は以下のエラーを返す。
155 グループやリストなどの、パターンオペレータの無効な使用。
158 \(aq*\(aq が最初の文字としてくるような、無効な繰り返しオペレータの使用。
161 インターバルオペレータ \fB{}\fP (brace interval operators) が閉じていない。
164 リストオペレータ \fB[]\fP (bracket list operators) が閉じていない。
167 照合順序の要素 (collating element) として有効ではない。 (訳注) 詳細は \fBregex\fP(7) を参照。
173 未定義エラー。これは POSIX.2 には定義されていない。
179 グループオペレータ \fB()\fP (parenthesis group operators) が閉じていない。
182 無効な範囲オペレータの使用。 例えば、範囲の終了位置が開始位置よりも前にあるような場合。
185 正規表現のコンパイルに、64\ Kb 以上のパターンバッファーが必要。 これは POSIX.2 には定義されていない。
188 regex ルーチンがメモリーを使いはたしている。
191 サブエクスプレッション \e\fB(...\e\fP) (subexpression) への無効な後方参照。
193 この節で使用されている用語の説明については、 \fBattributes\fP(7) を参照。
202 T} Thread safety MT\-Safe locale
205 T} Thread safety MT\-Safe env
208 T} Thread safety MT\-Safe
211 POSIX.1\-2001, POSIX.1\-2008.
219 #define ARRAY_SIZE(arr) (sizeof((arr)) / sizeof((arr)[0]))
221 static const char *const str =
222 "1) John Driverhacker;\en2) John Doe;\en3) John Foo;\en";
223 static const char *const re = "John.*o";
227 static const char *s = str;
229 regmatch_t pmatch[1];
232 if (regcomp(®ex, re, REG_NEWLINE))
235 printf("String = \e"%s\e"\en", str);
236 printf("Matches:\en");
238 for (int i = 0; ; i++) {
239 if (regexec(®ex, s, ARRAY_SIZE(pmatch), pmatch, 0))
242 off = pmatch[0].rm_so + (s \- str);
243 len = pmatch[0].rm_eo \- pmatch[0].rm_so;
244 printf("#%d:\en", i);
245 printf("offset = %jd; length = %jd\en", (intmax_t) off,
247 printf("substring = \e"%.*s\e"\en", len, s + pmatch[0].rm_so);
249 s += pmatch[0].rm_eo;
256 \fBgrep\fP(1), \fBregex\fP(7)
258 glibc マニュアルのセクション \fIRegular Expressions\fP
260 この man ページは Linux \fIman\-pages\fP プロジェクトのリリース 5.10 の一部である。プロジェクトの説明とバグ報告に関する情報は
261 \%https://www.kernel.org/doc/man\-pages/ に書かれている。