-1
如何修改此代码以便它找到正则表达式匹配的索引。基本上假设我有“金”作为我的测试表达。我的测试字符串是“jkimsdfs”。我怎么能证明“jkimsdfs”在索引与“金”匹配1.如何查找表达式的哪个索引与正则表达式匹配
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <regex.h>
int main() {
char word[100]; //sets the limit of characters within word to 99(0-99
char exp[100]; //sets the limit of characters within exp to 99(0-99)
regex_t w; //contains size_t. This field holds the number of
//parenthetical subexpressions in the regular expression that was compiled.
int status; // integer status
printf("Test Expression: "); //print "Test Expression"
fgets(word,100,stdin); //
word[strlen(word)-1] = '\0';
status = regcomp(&w,word,REG_EXTENDED|REG_NOSUB);
printf("%d\n", status);
printf("Validity of regex, if 0 than it matches: %d\n",status);
if (status) return 1;
while (1) {
printf("Test String: ");
fgets(exp,100,stdin);
exp[strlen(exp)-1] = '\0';
if (strlen(exp) < 1)
break;
status = regexec(&w,exp,(size_t)0,NULL,0);
printf("Matched: %d\n",status);
printf("%d\n", status);
}
regfree(&w);
return 0;
}
阅读['man regcomp'](http://man7.org/linux/man-pages/man3/regex.3.html)以了解REG_NOSUB的作用,然后将其从代码中移除。 – melpomene
另外,不是将[0],NULL传递给['regexec()'](http://man7.org/linux/man-pages/man3/regexec.3.html),而是传入元素的个数, 'regmatch_t'的数组。数组中的初始元素将代表整个匹配。 –