Class: Regex::Lookaround

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

Overview

Lookaround is a zero-width assertion just like the start and end of line anchors. The difference is that lookarounds will actually match characters, but only return the result of the match: match or no match. That is why they are called "assertions". They do not consume characters from the subject, but only assert whether a match is possible or not.

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(assertedExpression, theDir, theKind) ⇒ Lookaround

Constructor. [assertedExpression] A sub-expression to match. [theDir] One of the following values: [ :ahead, :behind ] [theKind] One of the following values: [ :positive, :negative ]



34
35
36
37
38
# File 'lib/regex/lookaround.rb', line 34

def initialize(assertedExpression, theDir, theKind)
  super(assertedExpression)
  @dir = theDir
  @kind = theKind
end

Instance Attribute Details

#dirObject (readonly)

The "direction" of the lookaround. Can be ahead or behind. It specifies the relative position of the expression to match compared to the current 'position' in the subject text.



23
24
25
# File 'lib/regex/lookaround.rb', line 23

def dir
  @dir
end

#kindObject (readonly)

The kind indicates whether the assertion is positive (succeeds when there is a match) or negative (assertion succeeds when there is NO match).



28
29
30
# File 'lib/regex/lookaround.rb', line 28

def kind
  @kind
end

Instance Method Details

#to_strObject

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



42
43
44
45
46
47
# File 'lib/regex/lookaround.rb', line 42

def to_str
  dir_syntax = (dir == :ahead) ? '' : '<'
  kind_syntax = (kind == :positive) ? '=' : '!'
  result = "(?#{dir_syntax}#{kind_syntax}#{child.to_str})"
  return result
end