使用regex.h的正则表达式 [英] Regular expressions using regex.h
问题描述
团队,
您能否建议一个站点,该站点实际上包含有关在Linux上的C语言中通过regex.h使用正则表达式的完整教程.
感谢您的回应.
问候,
Hi Team,
Can you please suggest a site which actually contains full tutorial about using regular expressions through regex.h in C on linux.
Appreciate your response.
Regards,
推荐答案
此处("C中的POSIX regex示例" [ ^ ])使用示例C
中的POSIX
正则表达式.这里是有关"10.3正则表达式匹配"的GNU
文档 [ ^ ]正则表达式图书馆.使用
Here ( "Example of POSIX regex in C"[^] ) an example of usingPOSIX
regular expressions inC
. Here theGNU
documentation about "10.3 Regular Expression Matching"[^] the regular expressionsC
library. With
不需要完整的教程,它们很容易使用.
请参见在线MAN页 [
No need for a full tutorial, they are simple to use.
See the online MAN page[^] for a full description
1. Compile an expression (this example is to match integer or decimal numbers)
2. Use that expression
3. Free the compiled expression when you are done with it
#include <sys/types.h>
#include <regex.h>
#include <stdio.h>
#define MAX_MATCHES 1 //The maximum number of matches allowed in a single string
void match(regex_t *pexp, char *sz) {
regmatch_t matches[MAX_MATCHES]; //A list of the matches in the string (a list of 1)
//Compare the string to the expression
//regexec() returns 0 on match, otherwise REG_NOMATCH
if (regexec(pexp, sz, MAX_MATCHES, matches, 0) == 0) {
printf("\"%s\" matches characters %d - %d\n", sz, matches[0].rm_so, matches[0].rm_eo);
} else {
printf("\"%s\" does not match\n", sz);
}
}
int main() {
int rv;
regex_t exp; //Our compiled expression
//1. Compile our expression.
//Our regex is "-?[0-9]+(\\.[0-9]+)?". I will explain this later.
//REG_EXTENDED is so that we can use Extended regular expressions
rv = regcomp(&exp, "-?[0-9]+(\\.[0-9]+)?", REG_EXTENDED);
if (rv != 0) {
printf("regcomp failed with %d\n", rv);
}
//2. Now run some tests on it
match(&exp, "0");
match(&exp, "0.");
match(&exp, "0.0");
match(&exp, "10.1");
match(&exp, "-10.1");
match(&exp, "a");
match(&exp, "a.1");
match(&exp, "0.a");
match(&exp, "0.1a");
match(&exp, "hello");
//3. Free it
regfree(&exp);
return 0;
}
现在,对正则表达式进行基本说明.
原始的正则表达式非常基本.其中包括:
Now for a basic explanation of regular expressions.
The original regular expressions were pretty basic. They included:
One of [AB] one of A or B
Not one of [^AB] anything but A and B (new line may or may not be included, implementation or option specific)
Zero or more A* any number of A's including 0
Group (A) Used for grouping things like OR
Any character . Any single character (not including new line)
请参见 Wikipedia [ ^ ]以获得完整列表
然后是扩展的正则表达式,它添加了许多有用的功能:
See Wikipedia[^] for a full list
Then came extended regular expressions, which added many useful features:
Zero or one A? Either zero or 1 A (same as "A\{0,1\}" in basic regex)
Or A|B Either A or B
One or more A+ One or more A's (same as "AA*" in basic regex)
请参见 Wikipedia [ ^ ]以获得完整列表
然后是预定义的字符列表,作为编写[ABC]
的简写,请参见 Wikipedia [ ^ ].这些可以是特定于实现的,但是GNU可以支持Wikipedia列出的所有集合.
构造正则表达式的最简单方法是将其分解为最简单的部分,然后将它们组合在一起.
例如,我们需要一个表达式,该表达式将查找以A开头,以B结尾或以B开头并以A结尾的任何单词.
首先,将其分为2个部分
1. A. * B
2. B. * A
现在,将它们与OR
分组在一起
(A. * B)|(B. * A)
请注意,这与[AB].*[AB]
不同,[AB].*[AB]
也将匹配AA,AxA,BB,...
See Wikipedia[^] for a full list
Then there are predefined lists of characters, as a shorthand of writing [ABC]
, see Wikipedia[^] for a full list. These can be implementation specific, however the GNU one supports all sets listed at Wikipedia.
The easiest way to build a regular expression is to break it down into its simplest parts, and them combine them together.
For example we want an expression that will find any words starting with A and ending with B OR starting with B and ending with A.
First, break it into its 2 components
1. A.*B
2. B.*A
Now group them together with an OR
(A.*B)|(B.*A)
Note that this is different to [AB].*[AB]
, which will also match AA, AxA, BB, ...
尝试查看文档:
http://gnuwin32.sourceforge.net/packages/regex.htm [
Try looking at the documentation:
http://gnuwin32.sourceforge.net/packages/regex.htm[^]
Examples are included with the source, and the documentation is fairly decent - for this pupose the GnuWin32 download it''s equally applicable to linux/*nix.
Most likely this is already somewhere on your system :)
Best regards
Espen Harlinn
这篇关于使用regex.h的正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!