C中的正则表达式匹配

时间:2018-02-02 03:26:21

标签: c regex

我需要制作一个可以匹配长度为<的任何字母数字字符串的正则表达式。 99由两个@封闭。 ' @'之后的第一个字符也可以是' _'我不知道如何解释。 防爆。 @ U001 @将有效。 @ _A111 @也有效。但是,@ _____ ABC @将无效,@ ABC也不会。

我对正则表达式相对较新,并注意到\ z是一个无法识别的转义序列。如果重要的话,我试图在C11中写它。

#include <regex.h>        
regex_t regex;
int reti;
char msgbuf[100];

/* Compile regular expression */
reti = regcomp(&regex, "^@[[:alnum:]]@\z", 0);
if (reti) {
    fprintf(stderr, "Could not compile regex\n");
    exit(1);
}

1 个答案:

答案 0 :(得分:1)

尝试使用以下模式:

^@[_[:alnum:]][:alnum:]{0,97}@

以下是模式的简要说明

^                from the start of the string
@                match @
[_[:alnum:]]     match underscore or alpha
[:alnum:]{0,97}  then match zero to 97 alpha
@                match @

代码:

reti = regcomp(&regex, "^@[_[:alnum:]][:alnum:]{0,97}@", 0);
相关问题