Class: PCRE2::MatchData

Inherits:
Object
  • Object
show all
Defined in:
lib/pcre2/matchdata.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(regexp, string, pairs) ⇒ MatchData

Returns a new instance of MatchData.



4
5
6
7
8
# File 'lib/pcre2/matchdata.rb', line 4

def initialize(regexp, string, pairs)
  @regexp = regexp
  @string = string
  @pairs = pairs
end

Instance Attribute Details

#pairsObject (readonly)

Returns the value of attribute pairs.



2
3
4
# File 'lib/pcre2/matchdata.rb', line 2

def pairs
  @pairs
end

#regexpObject (readonly)

Returns the value of attribute regexp.



2
3
4
# File 'lib/pcre2/matchdata.rb', line 2

def regexp
  @regexp
end

#stringObject (readonly)

Returns the value of attribute string.



2
3
4
# File 'lib/pcre2/matchdata.rb', line 2

def string
  @string
end

Instance Method Details

#[](key) ⇒ Object



10
11
12
13
14
15
16
17
18
# File 'lib/pcre2/matchdata.rb', line 10

def [](key)
  if !key.is_a?(Numeric)
    key = regexp.named_captures[key.to_s].first
  end

  if pair = pairs[key]
    string_from_pair(*pair)
  end
end

#capture_pairsObject



24
25
26
# File 'lib/pcre2/matchdata.rb', line 24

def capture_pairs
  pairs[1..-1]
end

#capturesObject



32
33
34
# File 'lib/pcre2/matchdata.rb', line 32

def captures
  to_a[1..-1]
end

#end_of_matchObject



52
53
54
# File 'lib/pcre2/matchdata.rb', line 52

def end_of_match
  offset(0)[1]
end

#lengthObject



36
37
38
# File 'lib/pcre2/matchdata.rb', line 36

def length
  start_of_match - end_of_match
end

#offset(n) ⇒ Object



20
21
22
# File 'lib/pcre2/matchdata.rb', line 20

def offset(n)
  pairs[n]
end

#post_matchObject



44
45
46
# File 'lib/pcre2/matchdata.rb', line 44

def post_match
  string[end_of_match .. -1]
end

#pre_matchObject



40
41
42
# File 'lib/pcre2/matchdata.rb', line 40

def pre_match
  string[0 ... start_of_match]
end

#start_of_matchObject



48
49
50
# File 'lib/pcre2/matchdata.rb', line 48

def start_of_match
  offset(0)[0]
end

#to_aObject



28
29
30
# File 'lib/pcre2/matchdata.rb', line 28

def to_a
  @to_a ||= pairs.map { |pair| string_from_pair(*pair) }
end