Module: RE2::String

Defined in:
lib/re2/string.rb

Instance Method Summary collapse

Instance Method Details

#re2_escapeString Also known as: re2_quote

Escapes all potentially meaningful regexp characters. The returned string, used as a regular expression, will exactly match the original string.

Examples:

"1.5-2.0?".escape    #=> "1\.5\-2\.0\?"

Returns:

  • (String)

    the escaped string



95
96
97
# File 'lib/re2/string.rb', line 95

def re2_escape
  RE2.QuoteMeta(self)
end

#re2_gsub(*args) ⇒ Object

Replaces every occurrence of pattern with rewrite and return a new string.

Examples:

"hello there".re2_gsub("e", "i")   #=> "hillo thiri"
re2 = RE2.new("oo?")
"whoops-doops".re2_gsub(re2, "e")  #=> "wheps-deps"
text = "Good morning"
text.re2_gsub("o", "ee")           #=> "Geeeed meerning"
text                               #=> "Good morning"

Parameters:

  • pattern (String, RE2::Regexp)

    a regexp matching text to be replaced

  • rewrite (String)

    the string to replace with



39
40
41
# File 'lib/re2/string.rb', line 39

def re2_gsub(*args)
  RE2.GlobalReplace(self, *args)
end

#match(pattern) ⇒ RE2::MatchData #match(pattern, 0) ⇒ Boolean #match(pattern, number_of_matches) ⇒ RE2::MatchData

Match the pattern and return either a boolean (if no submatches are required) or a MatchData instance.

Overloads:

  • #match(pattern) ⇒ RE2::MatchData

    Returns an MatchData containing the matching pattern and all subpatterns resulting from looking for pattern.

    Examples:

    r = RE2::Regexp.new('w(o)(o)')
    "woo".re2_match(r)             #=> #<RE2::MatchData "woo" 1:"o" 2:"o">

    Parameters:

    Returns:

    Raises:

    • (NoMemoryError)

      if there was not enough memory to allocate the matches

  • #match(pattern, 0) ⇒ Boolean

    Returns either true or false indicating whether a successful match was made.

    Examples:

    r = RE2::Regexp.new('w(o)(o)')
    "woo".re2_match(0) #=> true
    "bob".re2_match(0) #=> false

    Parameters:

    Returns:

    • (Boolean)

      whether the match was successful

    Raises:

    • (NoMemoryError)

      if there was not enough memory to allocate the matches

  • #match(pattern, number_of_matches) ⇒ RE2::MatchData

    See match(pattern) but with a specific number of matches returned (padded with nils if necessary).

    Examples:

    r = RE2::Regexp.new('w(o)(o)')
    "woo".re2_match(r, 1) #=> #<RE2::MatchData "woo" 1:"o">
    "woo".re2_match(r, 3) #=> #<RE2::MatchData "woo" 1:"o" 2:"o" 3:nil>

    Parameters:

    • pattern (String, RE2::Regexp)

      the regular expression to match

    • number_of_matches (Fixnum)

      the number of matches to return

    Returns:

    Raises:

    • (NoMemoryError)

      if there was not enough memory to allocate the matches

Returns:



84
85
86
# File 'lib/re2/string.rb', line 84

def re2_match(pattern, *args)
  RE2::Regexp.new(pattern).match(self, *args)
end

#re2_sub(*args) ⇒ Object

Replaces the first occurrence pattern with rewrite and returns a new string.

Examples:

"hello there".re2_sub("hello", "howdy") #=> "howdy there"
re2 = RE2.new("hel+o")
"hello there".re2_sub(re2, "yo")        #=> "yo there"
text = "Good morning"
text.re2_sub("morn", "even")            #=> "Good evening"
text                                    #=> "Good morning"

Parameters:

  • pattern (String, RE2::Regexp)

    a regexp matching text to be replaced

  • rewrite (String)

    the string to replace with



24
25
26
# File 'lib/re2/string.rb', line 24

def re2_sub(*args)
  RE2.Replace(self, *args)
end