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.



25
26
27
28
29
# File 'lib/regex/capturing_group.rb', line 25

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



12
13
14
# File 'lib/regex/capturing_group.rb', line 12

def id
  @id
end

#no_backtrackObject (readonly)

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



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

def no_backtrack
  @no_backtrack
end

Instance Method Details

#named?Boolean

Return true iff the capturing group has a name

Returns:

  • (Boolean)


32
33
34
# File 'lib/regex/capturing_group.rb', line 32

def named?
  id.kind_of?(String)
end

#to_strObject

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



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/regex/capturing_group.rb', line 38

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