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 does not support regexp in any standard C.
A regular expression is a sequence of characters used to match a pattern to a string. The expression can be used for searching text and validating input. Remember, a regular expression is not the property of a particular language. POSIX is a well-known library used for regular expressions in C.
scanf
allows regular expressions as far as I know
Unfortunately, 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 of scanf
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
'\n'
is an 'x'
, and '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
does not support regular expressions. It has limited support for character classes but that's not at all the same thing.
Never use scanf
, fscanf
, or sscanf
, because:
%s
) are unsafe in exactly the same way gets
is unsafe, i.e. they will cheerfully write past the end of the provided buffer and crash your program.You don't need regular expressions for this case; read a line at a time with getline
and stop when the line read is just "x". However, the standard (not ISO C, but POSIX) regular expression library routines are called regcomp
and regexec
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With