The short answer: scanf
does not handle regular expressions literally speaking.
If you want to use regular expressions in C, you could use the regex POSIX library. See the following question for a basic example on this library usage : Regular expressions in C: examples?
Now if you want to do it the scanf
way you could try something like
scanf("%*[ ]%ns%*[ ]\n",str);
Replace the n
in %ns
by the maximal number of characters to read from input stream.
The %*[ ]
part asks to ignore any spaces. You could replace the *
by a specific number to ignore a precise number of characters. You could add other characters between braces to ignore more than just spaces.
Not sure if the above scanf would work as spaces are also matched with the %s
directive.
I would definitely go with a fgets
call, then triming the surrounding whitespaces with something like the following: How do I trim leading/trailing whitespace in a standard way?