scanf函数的正则表达式 - Ç [英] scanf regex - C
问题描述
我需要读取一个字符,直到按以下顺序写的是:\\ NX \\ N:
(.....)\\ n
点¯x\\ n
\\ n是新行字符和(.....)可以是可以包括其它\\ n个字符的字符。
scanf函数使得普通的前pressions据我知道,但我不能使它读取字符串,直到这种模式。你能不能帮我scanf格式串中?
我是想是这样的:
字符输入[50000];
scanf函数(%[^(\\ NX \\ N)],输入);
,但它不工作
scanf函数
使得普通的前pressions据我所知
块引用>不幸的是,它不允许常规的前pressions:语法是误导性接近,但并没有什么甚至远程类似于
scanf函数
的实施正则表达式。所有这一切的存在是的字符类的正则表达式的,所以支持%[<东西>]
被隐式视为[<东西>] *
。这就是为什么你的scanf函数
的通话转化成阅读自由除其他字符'(',')','X'和'串\\ N'
。要解决手边的问题,你可以设置一个循环,读取字符输入字符。每当你收到了
的'\\ n'
,检查
- 您必须输入至少三个字符,你已经迄今所看到的,
- 该字符之前立即
的'\\ n'
是'X'
和- 这之前的字符在
'X'
是另一个的'\\ n'
如果所有上述是真实的,你已经达到了预期的输入序列的结束;否则,你的循环应该继续。
I needed to read a string until the following sequence is written: \nx\n :
(.....)\n x\n
\n is the new line character and (.....) can be any characters that may include other \n characters.
scanf allows regular expressions as far as I know, but i can't make it to read a string untill this pattern. Can you help me with the scanf format string?
I was trying something like:
char input[50000]; scanf(" %[^(\nx\n)]", input);
but it doesn't work.
解决方案
scanf
allows regular expressions as far as I knowUnfortunately, it does not allow regular expressions: the syntax is misleadingly close, but there is nothing even remotely similar to the regex in the implementation of
scanf
. All that's there is a support for character classes of regex, so%[<something>]
is treated implicitly as[<something>]*
. That's why your call ofscanf
translates into read a string consisting of characters other than'(', ')', 'x', and '\n'
.To solve your problem at hand, you can set up a loop that read the input character by character. Every time you get a
'\n'
, check that
- You have at least three characters in the input that you've seen so far,
- That the character immediately before
'\n'
is an'x'
, and- That the character before the
'x'
is another'\n'
If all of the above is true, you have reached the end of your anticipated input sequence; otherwise, your loop should continue.
这篇关于scanf函数的正则表达式 - Ç的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!