Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/71.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
C:从模式匹配中提取字符串和字符串终止的替代方法?_C_Regex_String_Pattern Matching_Posix - Fatal编程技术网

C:从模式匹配中提取字符串和字符串终止的替代方法?

C:从模式匹配中提取字符串和字符串终止的替代方法?,c,regex,string,pattern-matching,posix,C,Regex,String,Pattern Matching,Posix,我的问题有两个方面: 我正在使用POSIX正则表达式,并且有一个非常好的系统来设置模式匹配,并且正在工作,但是很难处理C对字符串操作的低级/难以使用的支持,特别是在子字符串方法部门。我知道下面的例子不是一个小插曲,所以请耐心听我说,但我觉得这一节展示了我如何设置POSIX正则表达式以及如何使用它。我的主要问题是,如何从POSIX中提取每个模式匹配的字符串,并将其放入一个字符串/字符数组中,这样我就可以安全地使用普通的C字符串函数,比如这里的函数:?似乎我可以从模式匹配的内部结构中获取字符串,但是

我的问题有两个方面:

我正在使用POSIX正则表达式,并且有一个非常好的系统来设置模式匹配,并且正在工作,但是很难处理C对字符串操作的低级/难以使用的支持,特别是在子字符串方法部门。我知道下面的例子不是一个小插曲,所以请耐心听我说,但我觉得这一节展示了我如何设置POSIX正则表达式以及如何使用它。我的主要问题是,如何从POSIX中提取每个模式匹配的字符串,并将其放入一个字符串/字符数组中,这样我就可以安全地使用普通的C字符串函数,比如这里的函数:?似乎我可以从模式匹配的内部结构中获取字符串,但是每次我在模式匹配中执行我认为正常的子字符串操作时,C都会死掉

下面的示例显示了以前可以工作的代码,但直到我决定将变量“result”从数组更改为(char*)(您可以看到注释掉的旧方法),并尝试使用C的内存分配方法来正确执行此操作。您可以在输出中看到它走了多远,变量“result”显示正确,但是当我使用strstr(…)时,程序在IF比较时崩溃,原因我无法确定

我的最后一个问题,空终止(char*)的最佳方法是什么?显然,我在下面做这件事的方式可能是个问题

代码:(注意,“存储”是一个大尺寸的字符数组,其中包含正在进行模式匹配的文本)

regex\u t r;//存储正则表达式
regmatch_t m[50];//存储与正则表达式匹配的部分文件字符串
常量字符*p=存储;//指向将由regexec()读入的字符串的指针
字符匹配[tracker][BUFFSIZE];//包含字符串集合的二维数组
int ind=0;//索引变量“匹配”数组
printf(“收集模式匹配项”);
int regexer1=regcomp(&r,“
strcpy(result,(“%.*s\n)”,strcspn((p+start),“>”),strstrstr(p+start),“嘿,Ben,sprintf(…)太棒了。这正是我想要的将字符串以某种方式格式化为(char*)。有趣的是,它存在于可用的
之外,因为它来自
,但它起作用了。我认为逗号运算符起作用了。它肯定有助于我找到解决方案。听听您对strspn(…)的想法会很有趣但你的提议确实解决了一个问题,使我的程序崩溃。
regex_t r;  // stores regex
regmatch_t m[50];  // stores parts of file-string that matched the regex
const char * p = storage; // pointer to string that will be read in by regexec(...)
char matches[tracker][BUFFSIZE]; // 2D array containing a collection of strings
int ind = 0;  // indexing variable "matches" array
printf("### Collecting Pattern Matches ###\n");
int regExErr1 = regcomp(&r, "<[^<>]+=[[:space:]]*\"[^\"]+\"", REG_EXTENDED|REG_NEWLINE);
if( regExErr1 ){ 
    fprintf(stderr, "Fail to compile regex!\n"); 
    exit(1); 
}
while(1){
    regExErr1 = regexec(&r, p, 10, m, 0);
    if( regExErr1 != 0 ){ 
        fprintf(stderr, "Done finding URL pattern matches...\n"); 
        break; 
    }   
    int i = 0;
    while(1){
        if(m[i].rm_so == -1){
            break;
        }
        printf("entering loop at index %i\n", i);
        int start = m[i].rm_so;
        int finish = m[i].rm_eo;
        //char result[(finish - start)];
        char * result = (char *) malloc(strcspn(strstr(p + start, "<"), ">"));
        //strcpy(result, strstr(("%.*s\n", (finish - start), p + start), "<"));
        strcpy(result,("%.*s\n", strcspn((p+ start), ">"), strstr(p + start, "<")));
        result[strcspn(result, ">")+1] = 0;
        printf("LOOKING AT:  %s\n", result);
        if(strstr(result, "href") != NULL || strstr(result, "HREF") != NULL || strstr(result, "src") != NULL){
            printf("## CONSIDERING:  %s\n", result);
            if(strstr(result, "http:") == NULL && strstr(result, "mailto") == NULL){
                printf("Pattern is a relative URL.\n");
                strcpy(result, strstr(result, "\"") + 1);
                result[strcspn(result, "\"")] = 0;
                strcpy(result, relativePathCondense(result, "."));
                strcpy(matches[ind], base);
                strcat(matches[ind], result);
                matches[ind][(strlen(base) + strlen(result))] = 0; // NULL terminate the string match in the collection
                printf("Stored %i ==  %s\n", ind, matches[ind]);
                ...
                ind++; // update the counter to the 2D record array "matches"
            }else if(strstr(result, "http:") != NULL || strstr(result, "mailto:") != NULL){
                printf("Pattern is an absolute URL.\n");
                strcpy(result, strstr(result, "\"") + 1);
                result[strcspn(result, "\"")] = 0;
                printf("Trimmed expression is %s\n", result);
                strcpy(matches[ind], result);
                matches[ind][strlen(result)] = 0; // NULL terminate the string match in the collection
                printf("Stored %i ==  %s\n", ind, matches[ind]);
                ...
                ind++;                  
            }
        }
        i++;
    }
    p += m[0].rm_eo; // this will move the pointer p to the end of last matched pattern and on to the start of a new one
}
### Collecting URL's from stored HTML source document! ###
entering loop at index 0
LOOKING AT:  <BODY BGCOLOR = "#FFFFF0">
Segmentation fault (core dumped)
    strcpy(result,("%.*s\n", strcspn((p+ start), ">"), strstr(p + start, "<")));