Welcome, Guest! Registration

loc2log

Friday, 2019-02-22
Main » 2015 » July » 1 » C regex "Invalid regular expression"
6:50 PM
C regex "Invalid regular expression"

If you are thrown at legacy C code and see "Invalid regular expression" from regexec -- don't rush to blame your regular expression. The root cause for the "Invalid regular expression" could be improper set of flags used with the regexec call.

Synapsis

You have a simple regex. regcomp does compile the regex just fine. But when you get to regexec - it fails with "Invalid regular expression", and the regexec call uses some flags like this:

regexec(compiled_regex, subj_string, 0, NULL, REG_NOSUB | REG_ICASE); // the flags are WRONG!

Solution

Get rid of improper flags.

The only acceptable flags to the regexec are:

REG_NOTBOL - Do not regard the beginning of the specified string as the beginning of a line; more generally, don’t make any assumptions about what text might precede it.

REG_NOTEOL - Do not regard the end of the specified string as the end of a line; more generally, don’t make any assumptions about what text might follow it.

REG_NOSUB, REG_ICASE or whatever else - are supposed to be used with regcomp

For details see regex.h and/or Matching a Compiled POSIX Regular Expression.
Views: 667 | Added by: ep | Tags: Gotchas, C language, regex | Rating: 0.0/0
Total comments: 0
Only registered users can add comments.
[ Registration | Login ]