在 sscanf 中转义方括号 ]
我想像
"[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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您需要
注意三个连续的
]
字符 - 第一个是您不想在%[
集中匹配的字符,第二个是从%[
和第三个匹配输入中的]
字符You want
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正如其他人提到的......我不太确定你想在这里做什么......
假设括号之间的东西总是整数,你可以这样做:
如果你真的坚持使用正则表达式并想要一些在括号之间传递通用内容,您要寻找的是:
第二个正则表达式应该匹配除末尾的
]
之外的所有字符。我相信您之前尝试转义]
,但这不是必需的,编译器会抛出错误,因为它不知道如何处理该转义。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:
If you're really insistent about using regex and wanting some generic stuff passed in between the brackets, what you're looking for is:
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 fora2
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.
看起来您的问题基本上可以归结为“如何将
]
字符包含到scanf
扫描集中”。这是可能的,你不需要逃避它。为此,语言规范专门规定,当]
字符紧跟在开头[
字符后面或紧跟在开头^
字符后面时,< code>] 被认为是扫描集的一部分,而不是右括号。因此,在您的情况下,格式说明符应该看起来为"[%[^,], %[^]]] => %i"
。不需要\
。我发现您几乎做对了,只是您忘记了第二个扫描集之后的第三个
]
字符。It look like your question basically boils down to "how to include the
]
character into ascanf
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.在我看来,这听起来像是一个编译器(或者实际上是库)错误(尽管您确实需要第三个右括号,而不仅仅是两个)。根据标准(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):
尝试:
Try: