Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 如何在GNU正则表达式中检查字符串是否包含多个字符?_Regex - Fatal编程技术网

Regex 如何在GNU正则表达式中检查字符串是否包含多个字符?

Regex 如何在GNU正则表达式中检查字符串是否包含多个字符?,regex,Regex,您好,我想用GNU正则表达式检查字符串是否包含“abc”,我尝试了\b和\w,但两者都不起作用 #include <sys/types.h> #include <regex.h> #include <stdio.h> #include <stdlib.h> #include <sys/types.h> int main(int argc, char *argv[]){ regex_t regex; in

您好,我想用GNU正则表达式检查字符串是否包含“abc”,我尝试了
\b
\w
,但两者都不起作用

#include <sys/types.h>
#include <regex.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>

int main(int argc, char *argv[]){
        regex_t regex;
        int reti;
        char msgbuf[100];

/* Compile regular expression */
        //reti = regcomp(&regex, "\wabc\w", 0);
        reti = regcomp(&regex, "\babc\b", 0);
        if( reti ){ fprintf(stderr, "Could not compile regex\n"); exit(1); }

/* Execute regular expression */
        reti = regexec(&regex, "123abc123", 0, NULL, 0);
        if( !reti ){
                puts("Match");
        }
        else if( reti == REG_NOMATCH ){
                puts("No match");
        }
        else{
                regerror(reti, &regex, msgbuf, sizeof(msgbuf));
                fprintf(stderr, "Regex match failed: %s\n", msgbuf);
                exit(1);
        }

/* Free compiled regular expression if you want to use the regex_t again */
    regfree(&regex);

        return 0;
}
#包括
#包括
#包括
#包括
#包括
int main(int argc,char*argv[]){
regex_t regex;
国际视网膜;
char-msgbuf[100];
/*编译正则表达式*/
//reti=regcomp(®ex,“\wabc\w”,0);
reti=regcomp(®ex,“\babc\b”,0);
if(reti){fprintf(stderr,“无法编译正则表达式”);退出(1);}
/*执行正则表达式*/
reti=regexec(®ex,“123abc123”,0,NULL,0);
如果(!reti){
看跌期权(“匹配”);
}
else if(reti==REG_NOMATCH){
看跌期权(“不匹配”);
}
否则{
regerror(reti,®ex,msgbuf,sizeof(msgbuf));
fprintf(stderr,“正则表达式匹配失败:%s\n”,msgbuf);
出口(1);
}
/*如果要再次使用正则表达式,请释放已编译的正则表达式*/
regfree(®ex);
返回0;
}

要搜索字符串
abc
,只需删除
\b
,即

    reti = regcomp(&regex, "abc", 0);
这将匹配,而
“\babc\b”
将只匹配带有退格字符的
abc
。要仅匹配单词
abc
(即
123 abc 123
,但不匹配
123 abc123
123abc123
),请引用反斜杠,如中所示

    reti = regcomp(&regex, "\\babc\\b", 0);