Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use scanf with Regular Expressions

I've been trying to use regular expressions on scanf, in order to read a string of maximum n characters and discard anything else until the New Line Character. Any spaces should be treated as regular characters, thus included in the string to be read. I've studied a Wikipedia article about Regular Expressions, yet I can't get scanf to work properly. Here is some code I've tried:

scanf("[ ]*%ns[ ]*[\n]", string);

[ ] is supposed to go for the actual space character, * is supposed to mean one or more, n is the number of characters to read and string is a pointer allocated with malloc. I have tried several different combinations; however I tend to get only the first word of a sentence read (stops at space character). Furthermore, * seems to discard a character instead of meaning "zero or more"...

Could anybody explain in detail how regular expressions are interpreted by scanf? What is more, is it efficient to use getc repetitively instead?

Thanks in Advance :D

like image 239
someone Avatar asked Feb 14 '13 11:02

someone


People also ask

Does scanf use regular expression?

scanf does not support regexp in any standard C.

Can you use regex in C?

A regular expression is a sequence of characters that is used to search pattern. It is mainly used for pattern matching with strings, or string matching, etc. They are a generalized way to match patterns with sequences of characters. It is used in every programming language like C++, Java, and Python.

What does '$' mean in regex?

$ means "Match the end of the string" (the position after the last character in the string).

What does $1 do in regex?

For example, the replacement pattern $1 indicates that the matched substring is to be replaced by the first captured group.


2 Answers

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?

like image 161
greydet Avatar answered Nov 08 '22 02:11

greydet


is it efficient to use getc repetitively instead?

Depends somewhat on the application, but YES, repeated getc() is efficient.

like image 35
pmg Avatar answered Nov 08 '22 03:11

pmg