在C语言中使用正则表达式匹配字符串(忽略区分大小写)

在C语言中使用正则表达式匹配字符串(忽略区分大小写),c,regex,C,Regex,这是我的代码: #include <string.h> #include <ctype.h> #include <stdio.h> #include <regex.h> int main(void) { char name[]= "Michael Corleone"; char inputName[40]; regex_t regex; int return_value; print

这是我的代码:

#include <string.h>
#include <ctype.h>
#include <stdio.h>
#include <regex.h>

int main(void)
{
    char name[]= "Michael Corleone";
    char inputName[40];

    regex_t regex;
    int return_value;

    printf("Enter name: ");
    fgets(inputName, sizeof(inputName), stdin);
    // Remove new line from fgets
    inputName[strcspn(inputName, "\n")] = 0;
    
    // Regcomp string input by user as pattern
    return_value = regcomp(&regex, inputName, 0);
    // Regexec string that will match against user input
    return_value = regexec(&regex, name, 0, NULL, 0);

    if (return_value == REG_NOMATCH)
    {
        printf("Pattern not found.\n");
        return 1;
    }
    else
    {
        printf("%s\n", name);
    }
}
#包括
#包括
#包括
#包括
内部主(空)
{
字符名称[]=“迈克尔·科里昂”;
字符输入名[40];
regex_t regex;
int返回值;
printf(“输入名称:”);
fgets(inputName,sizeof(inputName),标准输入法);
//从FGET中删除新线
inputName[strcspn(inputName,“\n”)]=0;
//用户作为模式输入的Regcomp字符串
return_value=regcomp(®ex,inputName,0);
//将与用户输入匹配的Regexec字符串
return_value=regexec(®ex,name,0,NULL,0);
if(返回值==REG\U NOMATCH)
{
printf(“找不到图案。\n”);
返回1;
}
其他的
{
printf(“%s\n”,名称);
}
}
我尝试使用正则表达式匹配字符串。正如您所看到的,我的代码运行得非常好。数组中有一个名为Michael Corleone的个人存储。然后,当用户尝试输入:MichaelCorleoneMichael Corleone时,它将匹配并打印全名

但问题是区分大小写。如果用户尝试以小写形式输入这些名称,则将失败

我试着在
regcomp
中使用它:
regcomp(®ex,“[a-zA-Z][inputName]”,0)当我尝试用小写字母键入名称时,它会起作用。但后来我发现,当我键入另一个名字时,它也会起作用,比如约翰莱昂,或者天使。所以,我认为它匹配所有字母表


你们有解决办法吗?谢谢大家!

您需要将
regcomp
函数的最后一个参数(现在是
0
)替换为
regu-ICASE

return_value=regcomp(®ex,inputName,REG_-ICASE);//0已替换为注册表

从:

REG\u ICASE

不要区分大小写。使用此模式缓冲区的后续
regexec()
搜索不区分大小写