在 sscanf 中转义方括号 ]

发布于 2024-11-02 22:40:05 字数 1001 浏览 3 评论 0原文

我想像

"[25, 28] => 34"

我编写一个小程序一样扫描行来测试它:

#include <cstdlib>
#include <iostream>

int main() {
        char* line = "[25, 28] => 34";
        char a1[100],  a2[100];
        int i;
        sscanf(line, "[%[^,], %[^\]] => %i", a1, a2, &i);
        std::cout << "a1 = " << a1 <<"\na2 = " << a2 << "\ni = "<<i <<"\n";
        return 0;
}

编译此给出

warning: unknown escape sequence '\]'

和输出

a1 = 25
a2 = 28
i = -1073746244

如果我将其更改为

sscanf(line, "[%[^,], %[^]] => %i", a1, a2, &i);

我没有收到编译器投诉,但

a1 = 25
a2 = 28
i = -1073746244

我仍然知道问题出在第二个标记中,因为

sscanf(line, "[%[^,], %[0123456789]] => %i", a1, a2, &i);

给出

a1 = 25
a2 = 28
i = 34

但我想使用第二个令牌的终止条件。我该怎么做?

I want to scan lines like

"[25, 28] => 34"

I wrote a small program to test it out:

#include <cstdlib>
#include <iostream>

int main() {
        char* line = "[25, 28] => 34";
        char a1[100],  a2[100];
        int i;
        sscanf(line, "[%[^,], %[^\]] => %i", a1, a2, &i);
        std::cout << "a1 = " << a1 <<"\na2 = " << a2 << "\ni = "<<i <<"\n";
        return 0;
}

compiling this gives

warning: unknown escape sequence '\]'

and the output

a1 = 25
a2 = 28
i = -1073746244

If I change it to

sscanf(line, "[%[^,], %[^]] => %i", a1, a2, &i);

I get no compiler complaints but still

a1 = 25
a2 = 28
i = -1073746244

I know the problem is in the second token because

sscanf(line, "[%[^,], %[0123456789]] => %i", a1, a2, &i);

gives

a1 = 25
a2 = 28
i = 34

But I want to use a terminating condition for the second token. How do I do that?

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

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

发布评论

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

评论(5

好倦 2024-11-09 22:40:05

您需要

sscanf(line, "[%[^,], %[^]]] => %i", a1, a2, &i);

注意三个连续的 ] 字符 - 第一个是您不想在 %[ 集中匹配的字符,第二个是从%[ 和第三个匹配输入中的 ] 字符

You want

sscanf(line, "[%[^,], %[^]]] => %i", a1, a2, &i);

Note the three consecutive ] characters -- the first is the character you don't want to match in the %[ set, the second ends the set starting with the %[ and the third matches the ] character in the input

月依秋水 2024-11-09 22:40:05

正如其他人提到的......我不太确定你想在这里做什么......

假设括号之间的东西总是整数,你可以这样做:

int a, b, c;
sscanf(line, "[%d, %d] => %d", &a,&b,&c);

如果你真的坚持使用正则表达式并想要一些在括号之间传递通用内容,您要寻找的是:

sscanf(line, "[%[^,], %[^]]] => %d", a1, a2, &i);

第二个正则表达式应该匹配除末尾的 ] 之外的所有字符。我相信您之前尝试转义 ] ,但这不是必需的,编译器会抛出错误,因为它不知道如何处理该转义。

a2 的捕获子句中有 3 个 ],因为第一个表示“不要在字符匹配集中包含此字符”,第二个表示关闭字符匹配集,第三个匹配[25, 28] =>中的] 34输入。

如果我完全误解了你的问题,请澄清,我可以修改。

As others have mentioned... I'm not exactly sure what you want to do here...

Assuming that the things between the brackets are always integers, you could just do:

int a, b, c;
sscanf(line, "[%d, %d] => %d", &a,&b,&c);

If you're really insistent about using regex and wanting some generic stuff passed in between the brackets, what you're looking for is:

sscanf(line, "[%[^,], %[^]]] => %d", a1, a2, &i);

the second regex should be matching all characters except the ] at the end. I believe that you were trying to escape the ] before, but this isn't necessary and the compiler is throwing an error because it doesn't know how to handle that escape.

There are 3 ] in the capture clause for a2 because the first is saying, "don't include this character in the character match set," the second is closing off the character match set, and the third matches the ] in the [25, 28] => 34 input.

If I totally misunderstood your question, just clarify and I can revise.

凑诗 2024-11-09 22:40:05

看起来您的问题基本上可以归结为“如何将 ] 字符包含到 scanf 扫描集中”。这是可能的,你不需要逃避它。为此,语言规范专门规定,当 ] 字符紧跟在开头 [ 字符后面或紧跟在开头 ^ 字符后面时,< code>] 被认为是扫描集的一部分,而不是右括号。因此,在您的情况下,格式说明符应该看起来为 "[%[^,], %[^]]] => %i"。不需要\

我发现您几乎做对了,只是您忘记了第二个扫描集之后的第三个 ] 字符。

It look like your question basically boils down to "how to include the ] character into a scanf scanset". It is possible and you don't need to escape it. Specifically for that purpose the language specification states that when the ] character immediately follows the opening [ character or immediately follows the opening ^ character, that ] is considered to be a part of the scanset, not a closing bracket. So, in your case the format specifier is supposed to look as "[%[^,], %[^]]] => %i". No \ necessary.

I see that you almost got it right, except that you forgot the third ] character after the second scanset.

稀香 2024-11-09 22:40:05

在我看来,这听起来像是一个编译器(或者实际上是库)错误(尽管您确实需要第三个右括号,而不仅仅是两个)。根据标准(C99,§7.19.6.2):

如果转换说明符以 [] 或 [^] 开头,则右括号字符位于扫描列表中,并且下一个右括号字符是结束规范的匹配右括号;

It sounds to me like a compiler (or, actually, library) bug (though you do need a third right-bracket, not just two). According to the standard (C99, §7.19.6.2):

If the conversion specifier begins with [] or [^], the right bracket character is in the scanlist and the next following right bracket character is the matching right bracket that ends the specification;

站稳脚跟 2024-11-09 22:40:05

尝试:

sscanf(line, "[%s, %s] => %d", a1, a2, &i);

Try:

sscanf(line, "[%s, %s] => %d", a1, a2, &i);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文