class PCRE2::Regexp

Attributes

source[RW]

Public Class Methods

new(pattern, *options) click to toggle source

Accepts a String, Regexp or another PCRE2::Regexp

# File lib/pcre2/regexp.rb, line 8
def initialize(pattern, *options)
  case pattern
  when ::Regexp, PCRE2::Regexp
    @source = pattern.source
  else
    @source = pattern
  end

  @pattern_ptr = Lib.compile_pattern(source, options)
end

Public Instance Methods

jit!() click to toggle source

Compiles the Regexp into a JIT optimised version. Returns whether it was successful

# File lib/pcre2/regexp.rb, line 20
def jit!
  options = PCRE2_JIT_COMPLETE | PCRE2_JIT_PARTIAL_SOFT | PCRE2_JIT_PARTIAL_HARD

  Lib.pcre2_jit_compile_8(pattern_ptr, options) == 0
end
match(str, pos = nil) click to toggle source
# File lib/pcre2/regexp.rb, line 26
def match(str, pos = nil)
  result_count, match_data_ptr = Lib.match(@pattern_ptr, str, position: pos)

  if result_count == 0
    nil
  else
    pairs = PCRE2::Lib.get_ovector_pairs(match_data_ptr, result_count)

    MatchData.new(self, str, pairs)
  end
end
matches(str, pos = nil) { |matchdata| ... } click to toggle source
# File lib/pcre2/regexp.rb, line 38
def matches(str, pos = nil, &block)
  return enum_for(:matches, str, pos) if !block_given?

  pos ||= 0
  while pos < str.length
    matchdata = self.match(str, pos)

    if matchdata
      yield matchdata

      beginning, ending = matchdata.offset(0)

      if pos == ending # Manually increment position if no change to avoid infinite loops
        pos += 1
      else
        pos = ending
      end
    else
      return
    end
  end
end
named_captures() click to toggle source
# File lib/pcre2/regexp.rb, line 61
def named_captures
  @named_captures ||= Lib.named_captures(pattern_ptr)
end
names() click to toggle source
# File lib/pcre2/regexp.rb, line 65
def names
  named_captures.keys
end