C中的正則表達式:示例?下面是一些關于如何在ANSI C中使用正則表達式的簡單示例和最佳實踐。man regex.h并沒有提供那么多幫助。
3 回答

寶慕林4294392
TA貢獻2021條經驗 獲得超8個贊
#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);
grep
, sed
, vi
- 3 回答
- 0 關注
- 776 瀏覽
添加回答
舉報
0/150
提交
取消