Search code examples
c++cregexpcre

What's the difference between regexec and pcre_exec?


I have a pattern

"^\+?\d{3,20}$"
  • Test on "123455", "+123445", expected match.
  • Test on "123+213", "abc", expect no match.

This pattern worked on pcre_exec() but not on regexec().


Solution

  • The regexec() function implements POSIX Basic Regular Expressions (BRE) and Extended Regular Expressions (ERE).

    The pcre_exec() function implements Perl-Compatible Regular Expressions, which are a major superset of the ERE (see perlre for Perl's specification of Perl's REs).

    The \d notation is not special to POSIX ERE (let alone BRE); it does not mean 'a digit'. It does mean 'a digit' in PCRE. If you want the regular expression to work in both dialects, you'll need to use [0-9] in place of \d, and ensure regcomp() is told to compile an ERE.

    "^+?[0-9]{3,20}$"