0

我在 C 中尝试了一些正则表达式的东西,即尝试处理文本文件中的一行:

MY NAME IS   ;;JOHN 

我在 sscanf 的格式参数中使用sscanf并匹配。%s %s %s %[^;]当我用Rubular进行测试时(也就是说,我输入[^;]在“您的正则表达式:”字段和;;test“您的测试字符串:”字段中输入),它与test数据匹配。

然而,随着线

sscanf("MY NAME IS ;;JOHN", "%s %s %s %[^;], str1, str2, str3, str4),

第四个字符串str4实际上不匹配。也就是说,sscanf返回3。据我所知,正则表达式[^;]匹配分号以外的任何字符,那么为什么它似乎sscanf不匹配任何字符;;JOHN任何字符?

这只是一个问题sscanf,还是 glibc 出于某种原因会以不同的方式执行正则表达式?是那个吗sscanf因为字符串中的第一个字符确实是分号,所以无法匹配第四个字符串,所以它不能完全匹配字符串?这就是它返回 3 的原因吗?

此外,如果我有一行喜欢MY NAME IS DOE;;JOHN和使用sscanf,该函数确实返回 4。

4

1 回答 1

2

Here, see what the manual page for sscanf() says about regular expressions:

To be clear, the quote above is empty since the word "regular" doesn't appear in the manual page. This is because the patterns used with sscanf() and friends are not regular expressions. You seem to expect them to be, which creates the confusion.

The character range syntax %[] is described like so:

[ Matches a nonempty sequence of characters from the specified set of accepted characters; the next pointer must be a pointer to char, and there must be enough room for all the characters in the string, plus a terminating null byte.

The usual skip of leading white space is suppressed. The string is to be made up of characters in (or not in) a particular set; the set is defined by the characters between the open bracket [ character and a close bracket ] character.

The set excludes those characters if the first character after the open bracket is a circumflex (^). To include a close bracket in the set, make it the first character after the open bracket or the circumflex; any other position will end the set.

The hyphen character - is also special; when placed between two other characters, it adds all intervening characters to the set. To include a hyphen, make it the last character before the final close bracket. For instance, [^]0-9-] means the set "everything except close bracket, zero through nine, and hyphen". The string ends with the appearance of a character not in the (or, with a circumflex, in) set or when the field width runs out.

于 2013-04-25T06:46:21.157 回答