从字符串中提取未知子字符串

发布于 2025-01-02 15:16:50 字数 328 浏览 2 评论 0原文

我有一个程序以以下格式返回数据:

<CFData 0x1001219c0 [0x7fff7027aee0]>{length = 20, capacity = 20, bytes = 0x8deead13b8ae7057f6a629fdaae5e1200bcb8cf5}

我需要提取 8deead13b8ae7057f6a629fdaae5e1200bcb8cf5 (是的,减去 0x)。我尝试使用 sscanf 并传递一些正则表达式,但我对此一无所知。

知道如何做到这一点吗?代码片段值得赞赏。

I have a program returning data in the following format:

<CFData 0x1001219c0 [0x7fff7027aee0]>{length = 20, capacity = 20, bytes = 0x8deead13b8ae7057f6a629fdaae5e1200bcb8cf5}

I need to extract 8deead13b8ae7057f6a629fdaae5e1200bcb8cf5 (yes, minus the 0x). I tried using sscanf and passing some regular expressions but I have no clue on that.

Any idea how to do this? Code snippets are appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

记忆里有你的影子 2025-01-09 15:16:50

您可以使用 strstr() 在输入中找到“bytes = 0x” string 并复制字符串的其余部分(从“bytes = 0x”末尾开始),最后一个字符除外:

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

int main()
{
    char* s = "<CFData 0x1001219c0 [0x7fff7027aee0]>{length = 20, "
              "capacity = 20, "
              "bytes = 0x8deead13b8ae7057f6a629fdaae5e1200bcb8cf5}";
    char* value = 0;
    const char* begin = strstr(s, "bytes = 0x");

    if (begin)
    {
        begin += 10; /* Move past "bytes = 0x" */
        value = malloc(strlen(begin)); /* Don't need 1 extra for NULL as not
                                          copy last character from 'begin'. */
        if (value)
        {
            memcpy(value, begin, strlen(begin) - 1);
            *(value + strlen(begin) - 1) = 0;
            printf("%s\n", value);
            free(value);
        }
    }
    return 0;
}

You could use strstr() to locate "bytes = 0x" in the input string and copy the remainder of the string (from the end of "bytes = 0x") except for the last character:

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

int main()
{
    char* s = "<CFData 0x1001219c0 [0x7fff7027aee0]>{length = 20, "
              "capacity = 20, "
              "bytes = 0x8deead13b8ae7057f6a629fdaae5e1200bcb8cf5}";
    char* value = 0;
    const char* begin = strstr(s, "bytes = 0x");

    if (begin)
    {
        begin += 10; /* Move past "bytes = 0x" */
        value = malloc(strlen(begin)); /* Don't need 1 extra for NULL as not
                                          copy last character from 'begin'. */
        if (value)
        {
            memcpy(value, begin, strlen(begin) - 1);
            *(value + strlen(begin) - 1) = 0;
            printf("%s\n", value);
            free(value);
        }
    }
    return 0;
}
橙味迷妹 2025-01-09 15:16:50

您可以使用 strtok 来实现这一目的。

int main(int argc, char* argv[]) {
    char s[] = "<CFData 0x1001219c0 [0x7fff7027aee0]>{length = 20, capacity = 20, bytes = 0x8deead13b8ae7057f6a629fdaae5e1200bcb8cf5}";
    const char *tok = "<>[]{}= ,";
    char* t = strtok(s, tok);
    int take_next = false;
    char * res;
    while (t) {
        if (take_next) {
            res = t+2;
            break;
        }
        take_next = !strcmp(t, "bytes");
        t = strtok(NULL, tok);
    }
    printf("%s\n", res);
    return 0;
}

请注意,这只是一个示例。您应该强烈考虑使用 strtok_r 重写它,因为 strtok 是不可重入的。

You can use strtok to do the trick.

int main(int argc, char* argv[]) {
    char s[] = "<CFData 0x1001219c0 [0x7fff7027aee0]>{length = 20, capacity = 20, bytes = 0x8deead13b8ae7057f6a629fdaae5e1200bcb8cf5}";
    const char *tok = "<>[]{}= ,";
    char* t = strtok(s, tok);
    int take_next = false;
    char * res;
    while (t) {
        if (take_next) {
            res = t+2;
            break;
        }
        take_next = !strcmp(t, "bytes");
        t = strtok(NULL, tok);
    }
    printf("%s\n", res);
    return 0;
}

Note that this is only a sample. You should strongly consider rewriting this using strtok_r, because strtok is not re-entrant.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文