diff --git a/ext/re2/re2.cc b/ext/re2/re2.cc index 41d5341..7372d85 100644 --- a/ext/re2/re2.cc +++ b/ext/re2/re2.cc @@ -1251,7 +1251,13 @@ static VALUE re2_regexp_scan(VALUE self, VALUE text) { c->input = new(nothrow) re2::StringPiece(StringValuePtr(text)); c->regexp = self; c->text = text; - c->number_of_capturing_groups = p->pattern->NumberOfCapturingGroups(); + + if (p->pattern->ok()) { + c->number_of_capturing_groups = p->pattern->NumberOfCapturingGroups(); + } else { + c->number_of_capturing_groups = 0; + } + c->eof = false; return scanner; diff --git a/spec/re2/scanner_spec.rb b/spec/re2/scanner_spec.rb index 89fddf1..b0d1744 100644 --- a/spec/re2/scanner_spec.rb +++ b/spec/re2/scanner_spec.rb @@ -45,6 +45,12 @@ expect(scanner.scan).to be_nil end + it "returns nil if the regexp is invalid" do + r = RE2::Regexp.new('???', :log_errors => false) + scanner = r.scan("Foo bar") + expect(scanner.scan).to be_nil + end + it "returns an empty array if the input is empty" do r = RE2::Regexp.new("") scanner = r.scan("")