Debian Codegraph @

lib/re2/regexp.rb

raw symbols SCIP
1 # frozen_string_literal: true
2
3 # re2 (https://github.com/mudge/re2)
4 # Ruby bindings to RE2, a "fast, safe, thread-friendly alternative to
5 # backtracking regular expression engines like those used in PCRE, Perl, and
6 # Python".
7 #
8 # Copyright (c) 2010, Paul Mucur (https://mudge.name)
9 # Released under the BSD Licence, please see LICENSE.txt
10
11
12 module RE2
13 class Regexp
14 # Match the pattern against any substring of the given `text` and return a
15 # {RE2::MatchData} instance with the specified number of submatches
16 # (defaults to the total number of capturing groups) or a boolean (if no
17 # submatches are required).
18 #
19 # The number of submatches has a significant impact on performance: requesting
20 # one submatch is much faster than requesting more than one and requesting
21 # zero submatches is faster still.
22 #
23 # @param [String] text the text to search
24 # @param [Hash] options the options with which to perform the match
25 # @option options [Integer] :submatches how many submatches to extract (0
26 # is fastest), defaults to the total number of capturing groups
27 # @return [RE2::MatchData, nil] if extracting any submatches
28 # @return [Boolean] if not extracting any submatches
29 # @raise [ArgumentError] if given a negative number of submatches
30 # @raise [NoMemoryError] if there was not enough memory to allocate the
31 # matches
32 # @raise [TypeError] if given non-numeric submatches or non-hash options
33 # @example
34 # r = RE2::Regexp.new('w(o)(o)')
35 # r.partial_match('woot') #=> #<RE2::MatchData "woo" 1:"o" 2:"o">
36 # r.partial_match('nope') #=> nil
37 # r.partial_match('woot', submatches: 1) #=> #<RE2::MatchData "woo" 1:"o">
38 # r.partial_match('woot', submatches: 0) #=> true
39 def partial_match(text, options = {})
40 match(text, Hash(options).merge(anchor: :unanchored))
41 end
42
43 # Match the pattern against the given `text` exactly and return a
44 # {RE2::MatchData} instance with the specified number of submatches
45 # (defaults to the total number of capturing groups) or a boolean (if no
46 # submatches are required).
47 #
48 # The number of submatches has a significant impact on performance: requesting
49 # one submatch is much faster than requesting more than one and requesting
50 # zero submatches is faster still.
51 #
52 # @param [String] text the text to search
53 # @param [Hash] options the options with which to perform the match
54 # @option options [Integer] :submatches how many submatches to extract (0
55 # is fastest), defaults to the total number of capturing groups
56 # @return [RE2::MatchData, nil] if extracting any submatches
57 # @return [Boolean] if not extracting any submatches
58 # @raise [ArgumentError] if given a negative number of submatches
59 # @raise [NoMemoryError] if there was not enough memory to allocate the
60 # matches
61 # @raise [TypeError] if given non-numeric submatches or non-hash options
62 # @example
63 # r = RE2::Regexp.new('w(o)(o)')
64 # r.full_match('woo') #=> #<RE2::MatchData "woo" 1:"o" 2:"o">
65 # r.full_match('woot') #=> nil
66 # r.full_match('woo', submatches: 1) #=> #<RE2::MatchData "woo" 1:"o">
67 # r.full_match('woo', submatches: 0) #=> true
68 def full_match(text, options = {})
69 match(text, Hash(options).merge(anchor: :anchor_both))
70 end
71 end
72 end