正则表达式从字符串中提取特定子字符串

时间:2022-09-13 11:41:12

I am trying to extract a substring from string in c. I was trying to copy this answer but I can't get the regex right.

我试图从c中的字符串中提取子字符串。我试图复制这个答案,但我无法正确使用正则表达式。

From the string

从字符串

"-u username@web.com -p password -P passphrase -t tun_dev0 www.google.ie"

“-u username@web.com -p password -P passphrase -t tun_dev0 www.google.ie”

I would like to extract tun_dev0. Also, the parameters in that string may be in any order and there may be a variable number of spaces after the -t.

我想提取tun_dev0。此外,该字符串中的参数可以是任何顺序,并且在-t之后可以有可变数量的空格。

#include <stdio.h>
int main() {
    char *parameters = "-u username@web.com -p password -P passphrase -t tun_dev0 www.google.ie";

    char tun[64];

    sscanf(parameters, "-t %63[^ *]", tun);

    fprintf(stderr, "\n%s\n\n",tun);

    return 0;
}

Note: The string is not the parameters provided to this program

注意:该字符串不是提供给该程序的参数

Edit

As per perreal's answer, here is my implementation, it appears to work but it seems to skip the first parameter.

根据perreal的答案,这是我的实现,它似乎工作,但它似乎跳过第一个参数。

/*Get the parameter value associated with the specified parameter i.e -t / -p */
char *get_parameter(char *parameters, char parameter)
{
    char *p = strtok(parameters, "-");
    while(p) {
        p = strtok(NULL, "-");
        if (NULL != p) {
            if (p[0] == parameter) {
                p = strtok(p, " ");
                p = strtok(NULL, " ");
                printf("%s\n", p); 
                return p;
                break;
            }
        } else {
            return NULL;
        }
    }
    return NULL;
}

int main() {
    char parameters[100] = "-u username@web.com -p password -P passphrase -t tun_dev0 www.google.ie";
    char *parameter_value;

    parameter_value = get_parameter(parameters,'u');

    if (parameter_value != NULL) {
        fprintf(stderr,"%s\n",parameter_value);
    } else {
        fprintf(stderr,"No matching param found!\n");
    }   
    return 0;
}

2 个解决方案

#1


1  

You can use strtok:

你可以使用strtok:

#include <stdio.h>
#include <string.h>
int main() {
    char parameters[] = "-u username@web.com -p password -P passphrase -t tun_dev0 www.google.ie";
    char *t = strtok(parameters, "-");
    while(t) {
        t = strtok(NULL, "-");
        if (t[0] == 't') {
            t = strtok(t, " ");
            t = strtok(NULL, " ");
            printf("%s\n", t); 
            break;
        }
    }   
    return 0;
}

You with strtok you can't parse nested options. But you can first get the - arguments and process them:

你用strtok你无法解析嵌套选项。但是你可以先得到 - 参数并处理它们:

#include <stdio.h>
#include <string.h>

int main() {
    char parameters[] = "-u username@web.com -p password -P passphrase -t tun_dev0 www.google.ie";
    char *t; 
    char *args[256] = {0}, **arg = args;
    for (t = strtok(parameters, "-"); t != NULL; t = strtok(NULL, "-"), arg++) {
        *arg = t;  
    }   
    for (arg = args; *arg; arg++) {
        for (t = strtok(*arg, " "); t != NULL; t = strtok(NULL, " ")) {
            printf("%s\n", t); 
        }
    }   
    return 0;
}

#2


0  

strtok() is the better way, but if you still want sscanf(),

strtok()是更好的方法,但如果你还想要sscanf(),

char tun[64];
const char *p = parameters;
while (*p) {
  if (1 == sscanf(p, "-t %63s", tun)) {
    fprintf(stderr, "\n%s\n\n",tun);
    break;
  }
  p++;
}

#1


1  

You can use strtok:

你可以使用strtok:

#include <stdio.h>
#include <string.h>
int main() {
    char parameters[] = "-u username@web.com -p password -P passphrase -t tun_dev0 www.google.ie";
    char *t = strtok(parameters, "-");
    while(t) {
        t = strtok(NULL, "-");
        if (t[0] == 't') {
            t = strtok(t, " ");
            t = strtok(NULL, " ");
            printf("%s\n", t); 
            break;
        }
    }   
    return 0;
}

You with strtok you can't parse nested options. But you can first get the - arguments and process them:

你用strtok你无法解析嵌套选项。但是你可以先得到 - 参数并处理它们:

#include <stdio.h>
#include <string.h>

int main() {
    char parameters[] = "-u username@web.com -p password -P passphrase -t tun_dev0 www.google.ie";
    char *t; 
    char *args[256] = {0}, **arg = args;
    for (t = strtok(parameters, "-"); t != NULL; t = strtok(NULL, "-"), arg++) {
        *arg = t;  
    }   
    for (arg = args; *arg; arg++) {
        for (t = strtok(*arg, " "); t != NULL; t = strtok(NULL, " ")) {
            printf("%s\n", t); 
        }
    }   
    return 0;
}

#2


0  

strtok() is the better way, but if you still want sscanf(),

strtok()是更好的方法,但如果你还想要sscanf(),

char tun[64];
const char *p = parameters;
while (*p) {
  if (1 == sscanf(p, "-t %63s", tun)) {
    fprintf(stderr, "\n%s\n\n",tun);
    break;
  }
  p++;
}