Class: Regex::CapturingGroup

Inherits:
MonadicExpression show all
Defined in:
lib/regex/capturing_group.rb

Overview

An association between a capture variable and an expression the subject text in the same serial arrangement

Instance Attribute Summary collapse

Attributes inherited from MonadicExpression

#child

Attributes inherited from Expression

#begin_anchor, #end_anchor

Instance Method Summary collapse

Methods inherited from MonadicExpression

#done!, #lazy!

Methods inherited from CompoundExpression

#atomic?

Methods inherited from Expression

#atomic?, #options

Constructor Details

#initialize(aChildExpression, theId = nil, noBacktrack = false) ⇒ CapturingGroup

Constructor.

Parameters:

  • aChildExpression (Regex::Expression)

    A sub-expression to match. When successful the matching text is assigned to the capture variable.

  • theId (String) (defaults to: nil)

    The id of the capture variable.

  • noBacktrack (Boolean) (defaults to: false)

    A flag that specifies whether the capturing group forbids backtracking requests from its parent expression.



29
30
31
32
33
# File 'lib/regex/capturing_group.rb', line 29

def initialize(aChildExpression, theId = nil, noBacktrack = false)
  super(aChildExpression)
  @id = theId
  @no_backtrack = noBacktrack
end

Instance Attribute Details

#idObject (readonly)

The capture variable id. It is a Fixnum when the capture group gets a sequence number, a String when it is an user-defined name



16
17
18
# File 'lib/regex/capturing_group.rb', line 16

def id
  @id
end

#no_backtrackObject (readonly)

When true, then capturing group forbids backtracking requests from its parent expression.



20
21
22
# File 'lib/regex/capturing_group.rb', line 20

def no_backtrack
  @no_backtrack
end

Instance Method Details

#named?Boolean

Return true iff the capturing group has a name

Returns:

  • (Boolean)


36
37
38
# File 'lib/regex/capturing_group.rb', line 36

def named?
  id.kind_of?(String)
end

#to_strObject

Conversion method re-definition. Purpose: Return the String representation of the captured expression.



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/regex/capturing_group.rb', line 42

def to_str
  prefix = named? ? "?<#{id}>" : ''
  atomic = no_backtrack ? '?>' : ''
  if child.is_a?(Regex::NonCapturingGroup)
    # Minor optimization
    suffix = child.child.to_str
  else
    suffix = child.to_str
  end
  "(#{atomic}#{prefix}#{suffix})"
end