I want to take input of a particular part of a string like
"First (helloWorld): last"
From that string I want to take input only "helloWorld" by regular expression. I am using
%*[^(] (%s):"
But that does not serve my purpose. Please somebody help me to solve this problem.
Sorry, no true regular expression parser in standard C.
Using the format in the
scanf()
family is not a full-fledged regular expression, but can do the job."%n"
tellssscanf()
to save the current scanning offset.But this approach fails on "First (): last". More code would be needed.
A pair of
strchr()
calls is better.Else one needs to use a not-part-of-the C-spec solution.
The format specifiers in the
scanf
family of functions are not generally considered to be a species of regular expression.However, you can do what you want something like this.
The last format specifier is not necessary in the context of the above
sscanf
, but would be useful if reading from a stream and you want it positioned at the end of the current line for the next read. Note that the newline is still left in the stream, though.Rather than use
fscanf
(orscanf
) to read from a stream directly, it's pretty much always better read a line withfgets
and then extract the fields of interest withsscanf
Sample run: