class SPARQL::Algebra::Operator::Regex

The SPARQL `regex` operator.

@example

(prefix ((ex: <http://example.com/#>)
         (rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>))
  (project (?val)
    (filter (regex ?val "GHI")
      (bgp (triple ex:foo rdf:value ?val)))))

@see www.w3.org/TR/sparql11-query/#funcex-regex @see www.w3.org/TR/xpath-functions/#func-matches

Constants

NAME

Public Class Methods

new(text, pattern, flags = RDF::Literal(''), **options) click to toggle source

Initializes a new operator instance.

@param [RDF::Term] text @param [RDF::Term] pattern @param [RDF::Term] flags @param [Hash{Symbol => Object}] options

any additional options (see {Operator#initialize})

@raise [TypeError] if any operand is invalid

Calls superclass method SPARQL::Algebra::Operator::Ternary::new
# File lib/sparql/algebra/operator/regex.rb, line 29
def initialize(text, pattern, flags = RDF::Literal(''), **options)
  super
end

Public Instance Methods

apply(text, pattern, flags = RDF::Literal(''), **options) click to toggle source

Matches `text` against a regular expression `pattern`.

@param [RDF::Literal] text

a simple literal

@param [RDF::Literal] pattern

a simple literal

@param [RDF::Literal] flags

a simple literal (defaults to an empty string)

@return [RDF::Literal::Boolean] `true` or `false` @raise [TypeError] if any operand is unbound @raise [TypeError] if any operand is not a simple literal

# File lib/sparql/algebra/operator/regex.rb, line 45
def apply(text, pattern, flags = RDF::Literal(''), **options)
  # @see https://www.w3.org/TR/xpath-functions/#regex-syntax
  raise TypeError, "expected a plain RDF::Literal, but got #{text.inspect}" unless text.is_a?(RDF::Literal) && text.plain?
  text = text.to_s
  # TODO: validate text syntax

  # @see https://www.w3.org/TR/xpath-functions/#regex-syntax
  raise TypeError, "expected a plain RDF::Literal, but got #{pattern.inspect}" unless pattern.is_a?(RDF::Literal) && pattern.plain?
  pattern = pattern.to_s
  # TODO: validate pattern syntax

  # @see https://www.w3.org/TR/xpath-functions/#flags
  raise TypeError, "expected a plain RDF::Literal, but got #{flags.inspect}" unless flags.is_a?(RDF::Literal) && flags.plain?
  flags = flags.to_s
  # TODO: validate flag syntax

  options = 0
  raise NotImplementedError, "unsupported regular expression flag: /s" if flags.include?(?s) # FIXME
  options |= Regexp::MULTILINE  if flags.include?(?m)
  options |= Regexp::IGNORECASE if flags.include?(?i)
  options |= Regexp::EXTENDED   if flags.include?(?x)
  RDF::Literal(Regexp.new(pattern, options) === text)
end