Class: NamedCaptureExtractor

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

Overview

NamedCaptureExtractor is a utility class for extracting named groups from regular expressions and converting them into symbol-keyed hashes for flexible pattern matching and data extraction.

Class Method Summary collapse

Class Method Details

.extract_named_group2(match_data) ⇒ Object



97
98
99
# File 'lib/markdown_exec.rb', line 97

def self.extract_named_group2(match_data)
  match_data&.named_captures&.transform_keys(&:to_sym)
end

.extract_named_groups(str, pattern) ⇒ Hash<Symbol, String>?

Extracts named groups from a regex match on the given string, converting them into a symbol-keyed hash.

Parameters:

  • str (String)

    the string to match against the provided regex pattern

  • pattern (Regexp, String)

    the regex pattern with named groups

Returns:

  • (Hash<Symbol, String>, nil)

    hash of named groups as symbols and their corresponding matches, or nil if no match is found



93
94
95
96
# File 'lib/markdown_exec.rb', line 93

def self.extract_named_groups(str, pattern)
  regexp = pattern.is_a?(Regexp) ? pattern : Regexp.new(pattern)
  str&.match(regexp)&.named_captures&.transform_keys(&:to_sym)
end