@@ -697,13 +697,24 @@ bool RE::PartialMatch(const char* str, const RE& re) {
697
697
void RE::Init (const char * regex) {
698
698
pattern_ = regex;
699
699
700
+ // NetBSD (and Android, which takes its regex implemntation from NetBSD) does
701
+ // not include the GNU regex extensions (such as Perl style character classes
702
+ // like \w) in REG_EXTENDED. REG_EXTENDED is only specified to include the
703
+ // [[:alpha:]] style character classes. Enable REG_GNU wherever it is defined
704
+ // so users can use those extensions.
705
+ #if defined(REG_GNU)
706
+ constexpr int reg_flags = REG_EXTENDED | REG_GNU;
707
+ #else
708
+ constexpr int reg_flags = REG_EXTENDED;
709
+ #endif
710
+
700
711
// Reserves enough bytes to hold the regular expression used for a
701
712
// full match.
702
713
const size_t full_regex_len = strlen (regex) + 10 ;
703
714
char * const full_pattern = new char [full_regex_len];
704
715
705
716
snprintf (full_pattern, full_regex_len, " ^(%s)$" , regex);
706
- is_valid_ = regcomp (&full_regex_, full_pattern, REG_EXTENDED ) == 0 ;
717
+ is_valid_ = regcomp (&full_regex_, full_pattern, reg_flags ) == 0 ;
707
718
// We want to call regcomp(&partial_regex_, ...) even if the
708
719
// previous expression returns false. Otherwise partial_regex_ may
709
720
// not be properly initialized can may cause trouble when it's
@@ -714,7 +725,7 @@ void RE::Init(const char* regex) {
714
725
// regex. We change it to an equivalent form "()" to be safe.
715
726
if (is_valid_) {
716
727
const char * const partial_regex = (*regex == ' \0 ' ) ? " ()" : regex;
717
- is_valid_ = regcomp (&partial_regex_, partial_regex, REG_EXTENDED ) == 0 ;
728
+ is_valid_ = regcomp (&partial_regex_, partial_regex, reg_flags ) == 0 ;
718
729
}
719
730
EXPECT_TRUE (is_valid_)
720
731
<< " Regular expression \" " << regex
0 commit comments