skynet
skynet

Reputation: 23

'?' not working in C Posix regular expressions

I am trying to extract a string within double quotes in C. For example:

String[100] = "Hi,\"This is awesome\" and \"So is this\""

I only need to find the first match, "This is awesome". And this is what I tried,

regcomp(&preg, "\"(.*?)\"", REG_EXTENDED);
regexec(&preg, String, 2, regm, 0);

where regm is the match array.

Instead of "This is awesome", I get "This is awesome" and "So is this" as the match.

Shouldn't the ? in (.*?) limit the reg exp to finding the first match?

Upvotes: 0

Views: 148

Answers (1)

R.. GitHub STOP HELPING ICE
R.. GitHub STOP HELPING ICE

Reputation: 215447

No. Apparently this .*? thing you've written is intended to be a "non-greedy repetition" and came from Perl or some other language (not sure which). It has no such special meaning in POSIX ERE; the ? is just redundant. Instead try the correct form: "\"([^\"]*)\""

The specification for POSIX ERE can be found here:

http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap09.html#tag_09_04

Upvotes: 5

Related Questions