ANSI Cで正規表現を使用する方法の簡単な例とベストプラクティスの後には、man regex.h
はあまり役に立ちません。Cの正規表現:例?
答えて
正規表現は実際にはANSI Cの一部ではありません。ほとんどの(すべて?)* nixesに付属しているPOSIX正規表現ライブラリについて話しているようです。ここで(thisに基づいて)CでPOSIXの正規表現を使用した例です:
#include <regex.h>
regex_t regex;
int reti;
char msgbuf[100];
/* Compile regular expression */
reti = regcomp(®ex, "^a[[:alnum:]]", 0);
if (reti) {
fprintf(stderr, "Could not compile regex\n");
exit(1);
}
/* Execute regular expression */
reti = regexec(®ex, "abc", 0, NULL, 0);
if (!reti) {
puts("Match");
}
else if (reti == REG_NOMATCH) {
puts("No match");
}
else {
regerror(reti, ®ex, msgbuf, sizeof(msgbuf));
fprintf(stderr, "Regex match failed: %s\n", msgbuf);
exit(1);
}
/* Free memory allocated to the pattern buffer by regcomp() */
regfree(®ex);
また、あなたがPCREをチェックアウトすることができます、C.ザ・Perlの構文でPerl互換の正規表現のためのライブラリがそのほとんどですJava、Python、および他の多くの言語で使用されているのと同じ構文です。 POSIX構文はなどgrep
で使用される構文、sed
、vi
、
私は第2のPCREの依存関係を避ける必要がない限り、いくつかの優れた構文拡張があり、非常に安定しています。少なくとも古いバージョンのLinuxでは、特定の入力文字列や特定の正規表現が "ほぼ"一致するか、多くの特殊文字が含まれていると、 "組み込みの"正規表現ライブラリはクラッシュするのが難しくありません。 – bdk
@Laurence意味regcompに0を渡す? regcompは、4つの異なるモードを表すために4つの整数値1,2,4,8を取るだけです。 – lixiang
@lixiang 'regcomp'の最後のパラメータ、' cflags'はビットマスクです。 http://pubs.opengroup.org/onlinepubs/009695399/functions/regcompから。html: "cflags引数は、以下のフラグのゼロ以上のビット単位のORです..."。もしあなたがORを0にすると、0になるでしょう。私は、 'regcomp'のLinuxマンページは、" cflagsがビット単位であるかもしれない、あるいは以下のうちの1つ以上であるかもしれない "と言います。 –
であるそれはあなたが望むものはおそらくありませんが、re2cのようなツールは、それが代替として書かれているANSI CにPOSIX(-ish)正規表現をコンパイルすることができますlex
の場合、このアプローチでは、本当に必要な場合は、速度の最終段階で柔軟性と読みやすさを犠牲にすることができます。
man regex.h
レポートregex.hのマニュアルエントリはありませんが、man 3 regex
は、パターンマッチングのためのPOSIX関数を説明するページを提供します。
同じ機能がThe GNU C Library: Regular Expression Matchingに記載されています。ここでは、GNU CライブラリがPOSIX.2インターフェイスとGNU Cライブラリの長年の間に両方をサポートしていることが説明されています。例えば
、引数は最初の引数として渡されたパターンに一致するとして、あなたは次のようなコードを使用することができ渡された文字列のプリントがどの仮想的なプログラムのために:regcomp()
の
#include <errno.h>
#include <regex.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void print_regerror (int errcode, size_t length, regex_t *compiled);
int
main (int argc, char *argv[])
{
regex_t regex;
int result;
if (argc < 3)
{
// The number of passed arguments is lower than the number of
// expected arguments.
fputs ("Missing command line arguments\n", stderr);
return EXIT_FAILURE;
}
result = regcomp (®ex, argv[1], REG_EXTENDED);
if (result)
{
// Any value different from 0 means it was not possible to
// compile the regular expression, either for memory problems
// or problems with the regular expression syntax.
if (result == REG_ESPACE)
fprintf (stderr, "%s\n", strerror(ENOMEM));
else
fputs ("Syntax error in the regular expression passed as first argument\n", stderr);
return EXIT_FAILURE;
}
for (int i = 2; i < argc; i++)
{
result = regexec (®ex, argv[i], 0, NULL, 0);
if (!result)
{
printf ("'%s' matches the regular expression\n", argv[i]);
}
else if (result == REG_NOMATCH)
{
printf ("'%s' doesn't the regular expression\n", argv[i]);
}
else
{
// The function returned an error; print the string
// describing it.
// Get the size of the buffer required for the error message.
size_t length = regerror (result, ®ex, NULL, 0);
print_regerror (result, length, ®ex);
return EXIT_FAILURE;
}
}
/* Free the memory allocated from regcomp(). */
regfree (®ex);
return EXIT_SUCCESS;
}
void
print_regerror (int errcode, size_t length, regex_t *compiled)
{
char buffer[length];
(void) regerror (errcode, compiled, buffer, length);
fprintf(stderr, "Regex match failed: %s\n", buffer);
}
最後の引数少なくともREG_EXTENDED
である必要があります。または、関数はの代わりにa\{3\}
(extended regular expressions)を使用する必要があります(おそらく使用すると思われる)。
POSIX.2には、ワイルドカード照合のための別の機能があります。fnmatch()
。正規表現をコンパイルすることやサブ表現に一致する部分文字列を取得することはできませんが、ファイル名がワイルドカードと一致するかどうかを確認するために非常に具体的です(例:FNM_PATHNAME
フラグを使用)。
- 1. C#正規表現の正規表現
- 2. 正規表現C#
- 3. C#正規表現
- 4. 正規表現C#
- 5. C#正規表現
- 6. C#正規表現
- 7. C正規表現
- 8. 正規表現C#
- 9. C++ C++コードに正規表現変換正規表現
- 10. 正規表現例外
- 11. C++正規表現:C#の正規表現は、C++のLinuxへの変換
- 12. 正規表現の正規表現の正規表現
- 13. 正規表現C#の正規表現グループが
- 14. C(PCRE)の正規表現
- 15. C#Unityの正規表現
- 16. Cでの正規表現
- 17. Cプリプロセッサマクロの正規表現
- 18. マルチラインヘッダの正規表現C#
- 19. C#正規表現のトラブル
- 20. Cマッチグループの正規表現
- 21. Cの正規表現パターン - ?? - ? - *
- 22. C#の正規表現 -
- 23. Objective Cの正規表現
- 24. C#の:正規表現
- 25. C#の正規表現:パターン
- 26. 正規表現の正規表現と ' -
- 27. 正規表現の正規表現データ
- 28. 正規表現の正規表現
- 29. 正規表現用の正規表現
- 30. 正規表現c#ファイルアップロードコントロール
ANSI Cにregexの組み込みサポートはありません。どの正規表現ライブラリを使用していますか? – Joe
[Rob Pike](http://en.wikipedia.org/wiki/Rob_Pike)は、彼と[Brian Kernighanの著書「The Practice of Programming」の正規表現の非常に有用なサブセットを受け入れる小さな正規表現文字列検索関数を書いた](http://en.wikipedia.org/wiki/Brian_Kernighan)共著。このディスカッション、正規表現のMatcher、Kernighan博士の記事を参照してください。http://www.cs.princeton.edu/courses/archive/spr09/cos333/beautiful.html –