是scanf函数的"正则表达式"支持一个标准? [英] Is scanf's "regex" support a standard?
问题描述
时scanf函数的正则表达式支持一个标准?我不能在任何地方找到答案。
Is scanf's "regex" support a standard? I can't find the answer anywhere.
这code工作在海湾合作委员会,但不是在Visual Studio中:
This code works in gcc but not in Visual Studio:
scanf("%[^\n]",a);
这是一个Visual Studio故障或GCC扩展?
It is a Visual Studio fault or a gcc extension ?
编辑:看起来像VS作品,但必须要考虑Linux和Windows之间的线差结束(\\ r \\ n)的
Looks like VS works, but have to consider the difference in line ends between Linux and Windows.(\r\n)
推荐答案
这是特定格式字符串的应的符合规范的实施做工精细。在 [
字符引入了一个扫描集匹配字符的非空集(与 ^
即扫描集是供给的字符的反转)。换句话说,格式说明%[^ \\ n]
应的每一个字符,这不是一个新行匹配。
That particular format string should work fine in a conforming implementation. The [
character introduces a scanset for matching a non-empty set of characters (with the ^
meaning that the scanset is an inversion of the characters supplied). In other words, the format specifier %[^\n]
should match every character that's not a newline.
从C99 7.19.6.2,微微转述:
From C99 7.19.6.2, slightly paraphrased:
的 [
格式说明匹配从一组预期的字符(scanset)组成的字符的非空序列。如果没有→
长度修改为present,相应的参数应该是指向一个字符数组大到足以接受序列的初始元素和终止空字符,将被自动添加。
The
[
format specifier matches a nonempty sequence of characters from a set of expected characters (the scanset). If nol
length modifier is present, the corresponding argument shall be a pointer to the initial element of a character array large enough to accept the sequence and a terminating null character, which will be added automatically.
如果一个→
长度修改为present,输入应是多字节字符序列,在最初的移位状态开始。每个多字节字符转换为宽字符仿佛到 mbrtowc
函数的调用,与由描述的转换状态的mbstate_t
对象初始化为零
之前的第一个多字节字符被转换。对应参数必须是指向数组的初始元素 wchar_t的
大到足以接受的顺序和终止空宽字符,将被自动添加。
If an l
length modifier is present, the input shall be a sequence of multibyte characters that begins in the initial shift state. Each multibyte character is converted to a wide character as if by a call to the mbrtowc
function, with the conversion state described by an mbstate_t
object initialized to zero
before the first multibyte character is converted. The corresponding argument shall be a pointer to the initial element of an array of wchar_t
large enough to accept the sequence and the terminating null wide 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) compose 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. 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; otherwise the first following right bracket character is the one 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.
这是可能的,如果MSVC无法正常工作,这是只是众多例子,微软要么不符合最新标准,或认为他们更好地了解一种: - )
It's possible, if MSVC isn't working correctly, that this is just one of the many examples where Microsoft either don't conform to the latest standard, or think they know better :-)
这篇关于是scanf函数的"正则表达式"支持一个标准?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!