0

I have the following regex:

^(?!avoid).*

I have checket that it works, using https://regex101.com/:

enter image description here

However, if I try to compile it using regcomp in a C program like this:

#include <regex.h>
#include <stdio.h>
#include <stdlib.h>
 
int main(void)
{
   regex_t preg;
   char    *pattern = "^(?!avoid).*";
   int     rc;
   char    buffer[100];
 
   if (0 != (rc = regcomp(&preg, pattern, REG_EXTENDED))) {
      regerror(rc, &preg, buffer, 100);
      printf("regcomp() failed with '%s'\n", buffer);
      exit(-1);
   }
   return 0;
}

I get the following error:

regcomp() failed with 'Invalid preceding regular expression'

What am I doing wrong? :)

fgalan
  • 11,732
  • 9
  • 46
  • 89
  • 3
    POSIX regex does not support lookarounds. See the top of [this post](https://stackoverflow.com/a/37988661/3832970) for the POSIX workaround for strings that do not start with some specific string. – Wiktor Stribiżew Apr 20 '22 at 14:46
  • Thanks for your feedback! Is regex.h the only regular expression library in C/C++? Maybe there are alternative libraries supporting lookarounds? – fgalan Apr 20 '22 at 14:48
  • 1
    Sure, for C++, the most common workaround is using Boost `boost::regex`. – Wiktor Stribiżew Apr 20 '22 at 14:49

0 Answers0