scanf函数中的扫描集

ANSI C语言标准向scanf函数增加了一种新特性，叫做扫描集。利用此特性可以解决一些处理文本时的棘手问题。

Alice,Bob

[
Matches a nonempty sequence of characters from a set of expected
characters (the scanset ). The corresponding argument shall be a
pointer to the initial character of an array large enough to accept
the sequence and a terminating null character, which will be added
automatically. The conversion specifier includes all subsequent
characters in the format string, up to and including the matching
right bracket ( ] ). The characters between the brackets (the
scanlist ) comprise the scanset, unless the character after the left
bracket is a circumflex ( ^ ), in which case the scanset contains all
characters that do not appear in the scanlist between the circumflex
and the right bracket. As a special case, if the conversion specifier
begins with [] or [^] , the right bracket character is in the scanlist
and the next right bracket character is the matching right bracket
that ends the specification. If a – character is in the scanlist and
is not the first, nor the second where the first character is a ^ ,
nor the last character, the behavior is implementation-defined.

scanf(“%[123]”,a); 11223344 a=”112233″
scanf(“%[^123]”,a); 4321 a=”4″
scanf(“%[^d]”,a); abcdef a=”abc”
scanf(“%[0-9]%[a-z]%s”,a,b,c); 456ab789 a=”456″
b=”ab”
c=”789″
scanf(“%[^\n]”,a); hello world a=”hello world”
scanf(“%[^@]@%[^.].%s”,a,b,c); someone@example.com a=”someone”
b=”example”
c=”com”
scanf(“%[^(](%*[^)])%s”,a,b); abc(def)ghi a=”abc”
b=”ghi”