• 正则表达式


    获取字符串中的参数:

    方法一:strtok()

    #include<stdio.h>
    #include<string.h>
    
    
    int main(void)
    {    
        char url[]="rtsp://223.99.188.13/TVOD/88888888/224/3221226214/102553956_0.smil?timeshift=1&startpos=20190129T053201Z";
        char  *mpath = url;
        char *temp = strtok(mpath,"?&");
        static int i = 0,n =0,m=0;
        char *split[i];
        char *tshift[n];
        char *spos[m];
        
        char *mURL;
        int timeshift;
        char *startpos; 
        while(temp)
        {
            split[i] = temp;
            temp = strtok(NULL,"?&");
            i++;
        }
        mURL = split[0];
        
        temp = strtok(split[1],"=");
        while(temp)
        {
            tshift[n] = temp;
            temp= strtok(NULL,"=");
            n++;
        }
        if(strcmp(tshift[0],"timeshift")==0)
            timeshift = atoi(tshift[1]);
        
        temp = strtok(split[2],"=");
        while(temp)
        {
            spos[m] = temp;
            temp= strtok(NULL,"=");
            m++;
        }
        if(strcmp(spos[0],"startpos")==0)
            startpos = spos[1];
        printf("%s
    %d
    %s
    ",mURL,timeshift,startpos);
        
        int y;
        int mon;
        int d;
        int h;
        int min;
        int s;
        sscanf(startpos, "%4d%02d%2dT%2d%2d%2dZ", &y, &mon, &d, &h, &min, &s);
        printf("%d年%d月%d日 %d:%d:%d
    ",y,mon,d,h,min,s);
        
        printf("url = %s 
    ",mpath);
        return 0;
    }

    方法二:sscanf()

    #include<stdio.h>
    #include<string.h>
    
    
    int main(void)
    {    
        char url[]="rtsp://223.99.188.13/TVOD/88888888/224/3221226214/102553956_0.smil?timeshift=1&startpos=20190129T053201Z";
        char  *mpath = url;
        int ret;
        char flag[32];
        char path[1024];
        char pos[32];
        ret = sscanf(url, "%[^?]?timeshift=%[^&]&startpos=%[^Z]", path, flag, pos);
        printf("path=%s, flag=%s, pos=%s
    ", path, flag, pos);
        
        strtok(mpath,"?");
        printf("url = %s 
    ",url);
        printf("mpath = %s 
    ",mpath);
    }
  • 相关阅读:
    如何快速方便的输出向量vector容器中不重复的内容
    System.IO.FileInfo.cs
    System.IO.FileSystemInfo.cs
    System.IO.FileAttributes.cs
    System.IO.StreamWriter.cs
    System.IO.TextWriter.cs
    System.IO.StreamReader.cs
    System.IO.FileStream.cs
    System.IO.FileOptions.cs
    System.IO.FileShare.cs
  • 原文地址:https://www.cnblogs.com/zy791976083/p/10478531.html
Copyright © 2020-2023  润新知