如何查找表达式的哪个索引与正则表达式匹配

时间:2017-05-30 18:06:03

标签: c regex regular-language

如何修改此代码,以便找到正则表达式匹配的索引。基本上假设我有“kim”作为我的测试表达。我的测试字符串是“jkimsdfs”。如何在索引1处显示“jkimsdfs”与“kim”匹配。

#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;
        }

2 个答案:

答案 0 :(得分:1)

您可以使用react-native bundle --dev false --platform ios --entry-file index.ios.js --bundle-output ios/main.jsbundle --assets-dest ./ios

strstr

答案 1 :(得分:0)

也许文档说要获得匹配位置

size_t     nmatch = 2;
regmatch_t pmatch[2];

status = regexec(&w, exp, nmatch, pmatch, 0);
if ( status == 0 )
{
    printf( "Matched \"%.*s\" at position %d.\n",
         pmatch[1].rm_eo - pmatch[1].rm_so, &exp[pmatch[1].rm_so],
         pmatch[1].rm_so);
}
else
    printf( "Match not found\n" );