开发者

extract string value from a string

gcc 4.4.3 c89

I have the following string

sip:12387654345443222118765@xxx.xxx.xxx.xxx

How can I extract just the 开发者_如何学Pythonnumber? I just want the number.

12387654345443222118765

Many thanks for any advice,


There are lots of ways to do it, if the string is well-formatted you could use strchr() to search for the : and use strchr() again to search for the @ and take everything in between.

Here is another method that looks for a continuous sequence of digits:

char *start = sipStr + strcspn(sipStr, "0123456789");
int len = strspn(start, "0123456789");

char *copy = malloc(len + 1);

memcpy(copy, start, len);
copy[len] = '\0'; //add null terminator

...
//don't forget to
free(copy);


It sounds like you want it as a numeric type, which is going to be difficult (it's too large to fit in an int or a long). In theory you could just do:

const char* original = "sip:12387654345443222118765@xxx.xxx.xxx.xxx";
long num = strtoul(original + 4, NULL, 10);

but it will overflow and strtoul will return -1. If you want it as a string and you know it's always going to be that exact length, you can just pull out the substring with strcpy/strncpy:

const char* original = "sip:12387654345443222118765@xxx.xxx.xxx.xxx";
char num[24];
strncpy(num, original + 4, 23);
num[23] = 0;

If you don't know it's going to be 23 characters long every time, you'll need to find the @ sign in the original string first:

unsigned int num_length = strchr(original, '@') - (original + 4);
char* num = malloc(num_length + 1);
strncpy(num, original + 4, num_length);
num[num_length] = 0;


Use a regular expression :)

#include <regex.h>
regcomp() // compile your regex
regexec() // run your regex
regfree() // free your regex

:)


Have a look into the strtok or strtok_r functions.


Here is something that will deal with a variable width substring, which doesn't care about the starting position of the substring. For instance, if string was iax2:xxx@xx.xx.xx.xx, it would still work. It will, however return NULL if either delimiter can't be found.

It uses strchr() to find the delimiters, which lets us know where to start copying and where to stop. It returns an allocated string, the calling function must free() the returned pointer.

I'm pretty sure this is what you want?

Note: Edited from original to be more re-usable and a bit saner.

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

char *extract_string(const char *str, const char s1, const char s2)
{
    char *ret = NULL, *pos1 = NULL, *pos2 = NULL;
    size_t len;

    if (str == NULL || s1 < 0 || s2 < 0)
        return NULL;

    pos1 = strchr(str, s1);
    pos2 = strchr(str, s2);
    if (! pos1 || ! pos2)
        return NULL;

    len = ((pos2 - str) - (pos1 - str) - 1);
    ret = (char *) malloc(len + 1);
    if (ret == NULL)
        return NULL;

    memcpy(ret, str + (pos1 - str) + 1, len);
    ret[len] = '\0';

    return ret;
}

int main(void)
{
    const char *string = "sip:12387654345443222118765@xxx.xxx.xxx.xxx";
    char *buff = NULL;

    buff = extract_string(string, ':', '@');
    if (buff == NULL)
        return 1;

    printf("The string extracted from %s is %s\n" , string, buff);

    free(buff);

    return 0;
}

You could easily modify that to not care if the second delimiter is not found and just copy everything to the right of the first. That's an exercise for the reader.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜