Skip to content

1.5.0: Add RE2::Set for searching a collection of patterns simultaneously

Compare
Choose a tag to compare
@mudge mudge released this 16 Oct 19:09
· 208 commits to main since this release
75951ac

Contributed by @pritambaral, add RE2::Set representing a collection of patterns that can be searched for simultaneously.

Patterns are added to a set with RE2::Set#add—returning the index of the pattern in the set—and then compiled with RE2::Set#compile. Calling RE2::Set#match with text will return an array of integer indices of matching patterns, e.g.

set = RE2::Set.new
set.add("abc") #=> 0
set.add("def") #=> 1
set.add("ghi") #=> 2
set.compile #=> true
set.match("abcdefghi") #=> [0, 1, 2]
set.match("ghidefabc") #=> [2, 1, 0]

Depending on the ABI version of the underlying re2 library, RE2::Set#match will raise exceptions if there are any errors when matching. For ABI version 0, no error information is output so RE2::Set#match can be passed an :exception option of false to silently return an empty array if there are any errors, e.g.

set = RE2::Set.new
set.match("abcdef", exception: false) #=> []