c - 列表未正确使用正则表达式管理的字符串

标签 c regex string file list

我正在努力使这段代码工作。它必须从文本文件中取出一行,使用正则表达式来捕获我需要的信息(IP、日期、“请求”和代码)。正则表达式系统工作,并且在 while 循环结束时我拥有了我想要的所有字符串。但是当我尝试将它们分配给结果列表中的字符串并尝试打印它们时,一切都变得疯狂。

这是我管理的线路的一个例子: "46.252.157.14 - - [18/Dec/2013:00:00:01 +0100] "GET/f0?fid=21&os=0&cdl=0&id=SOR HTTP/1.1"200 17823";

This is the output:
Line: 1 IP:46.252.157.14
         Data:[18/Dec/2013:00:00:01 +0100]
         REQ:"GET /f0?fid=21&os=0&cdl=0&id=SOR HTTP/1.1
         COD: 200 

Print List:

IP: 46.252.157.14 - - [18/Dec/2013:00:00:01 +0100] "GET /f0?fid=21&os=0&cdl=0&id=SOR   HTTP/1.1" 200 
Data: [18/Dec/2013:00:00:01 +0100] "GET /f0?fid=21&os=0&cdl=0&id=SOR HTTP/1.1" 200 
Req: "GET /f0?fid=21&os=0&cdl=0&id=SOR HTTP/1.1" 200 
Cod:  200 

IP: (null)
Data: (null)
Req: (null)
Cod: (null)

如您所见,字符串都打印正确,当我尝试打印列表中的字符串时出现问题。

这是代码:

#include <string.h>
#include <stdio.h>
#include <regex.h>
#include <stdlib.h>
struct results{
    char* IP;
    char* Data;
    char* Req;
    char* cod;
    struct results* next;
    struct results* back;
};
typedef struct results results;

regex_t regex_ip;
regex_t regex_data;
regex_t regex_req;
regex_t regex_cod;

char* regex_ip_re= "^[0-9]*[.][0-9]*[.][0-9]*[.][0-9]*";
char* regex_data_re="\\[.*?\\]";
char* regex_req_re="(\".*?)\"";
char* regex_cod_re="\\s[0-9]{3}\\s";
char* line=NULL;



char* match(regex_t* r,char* to_match){
    char* result;
    regmatch_t regmatch[strlen(line)];
    if (regexec(r, to_match, strlen(line),regmatch, 0) == 0)
    {
      int g = 0;
      for (g = 0; g < strlen(line); g++)
        {
          if (regmatch[g].rm_so == (size_t)-1)
            break;  // No more groups

          char sourceCopy[strlen(to_match) + 1];
          strcpy(sourceCopy, to_match);
          sourceCopy[regmatch[g].rm_eo] = 0;
          result=sourceCopy+regmatch[g].rm_so;
        }
    }
    return result;
}

void createnode(results* node){
    results *temp=malloc(sizeof(results));
    temp->next=NULL;
    temp->back=node;
    node->next=temp;
}

int main(){
    regcomp(&regex_ip,regex_ip_re, REG_EXTENDED|REG_NEWLINE);
    regcomp(&regex_data,regex_data_re, REG_EXTENDED|REG_NEWLINE);
    regcomp(&regex_req,regex_req_re,REG_EXTENDED|REG_NEWLINE);
    regcomp(&regex_cod,regex_cod_re,REG_EXTENDED|REG_NEWLINE);

    FILE *log;
    char* IP=;char* DATA;char* REQ;char* COD;
    results *output=malloc(sizeof(results));
    results *head=output;
    size_t len=NULL;
    log=fopen("./log.txt","r");
    FILE *out=fopen("./output.txt","a");
    int linenum=1;

    //This is where i think the problem is:
    while(!feof(log)&&linenum<2){
        getline(&line,&len,log);

            IP=match(&regex_ip,line);
            printf("Line: %d IP:%s\n",linenum,IP);
            output->IP=IP;

            DATA=match(&regex_data,line);
            printf("         Data:%s\n",DATA);
            output->Data=DATA;

            REQ=match(&regex_req,line);
            printf("         REQ:%s\n",REQ);
            output->Req=REQ;

            COD=match(&regex_cod,line);
            printf("         COD:%s\n",COD);;
            output->cod=COD;

            linenum++;
            createnode(output);
            output=output->next;
    }
    output=head;
    printf("\nPrint List:\n\n");
    while(output!=NULL){
        printf("IP: %s\nData: %s\nReq: %s\nCod:
    %s\n\n",output->IP,output->Data,output->Req,output->cod);
        output=output->next;
    }





    return 0;
}

即使我认为问题仅出在列表上,我也尽量做到尽可能清楚并报告了整个代码。对不起,如果它难以阅读。

谢谢;)

编辑,最终结果:

列表的声明。

struct results{
    char IP[15];
    char Data[30];
    char Req[700];
    char cod[5];
    struct results* succ;
    struct results* prec;
};
typedef struct results results;

现在是时候了

while(!feof(log)){
        getline(&line,&len,log);
        strcpy(output->IP,match(&regex_ip,line));
        strcpy(output->Data,match(&regex_data,line));
        strcpy(output->Req,match(&regex_req,line));
        strcpy(output->cod,match(&regex_cod,line));
        linenum++;
        creanodo(output);
        output=output->succ;
}
return testa;

最佳答案

您正在存储指向超出范围的 transient C 字符串 (sourceCopy) 的指针。

稍后访问它们是 UB。

创建字符串的副本 (malloc+strcpy),然后将它们存储在您的列表中。

关于c - 列表未正确使用正则表达式管理的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21337767/

相关文章:

c - 带有 GMP 库的斐波那契数列

可以使用 vulkan 信号量来等待内存传输吗?

c - C程序中出现段错误

c# - 正则表达式检查字符串是否仅包含数字并且长度为 11 个字符/数字?

c++ - 在 BSONArrayBuilder 中包含正则表达式元素

Android:如何使用名称从资源中获取字符串?

c# - 将逗号分隔的数字字符串转换为 List<int>?

c - 字母出现频率 - 比例过高

c# - 在 C# 中验证 FQDN

java - 在字符串中追加文本