@@ -697,13 +697,24 @@ bool RE::PartialMatch(const char* str, const RE& re) {
697697void RE::Init (const char * regex) {
698698 pattern_ = regex;
699699
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+
700711 // Reserves enough bytes to hold the regular expression used for a
701712 // full match.
702713 const size_t full_regex_len = strlen (regex) + 10 ;
703714 char * const full_pattern = new char [full_regex_len];
704715
705716 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 ;
707718 // We want to call regcomp(&partial_regex_, ...) even if the
708719 // previous expression returns false. Otherwise partial_regex_ may
709720 // not be properly initialized can may cause trouble when it's
@@ -714,7 +725,7 @@ void RE::Init(const char* regex) {
714725 // regex. We change it to an equivalent form "()" to be safe.
715726 if (is_valid_) {
716727 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 ;
718729 }
719730 EXPECT_TRUE (is_valid_)
720731 << " Regular expression \" " << regex
0 commit comments