Module: RegexpExamples

Defined in:
lib/regexp-examples/groups.rb,
lib/regexp-examples/parser.rb,
lib/regexp-examples/helpers.rb,
lib/regexp-examples/version.rb,
lib/regexp-examples/constants.rb,
lib/regexp-examples/repeaters.rb,
lib/regexp-examples/exceptions.rb,
lib/regexp-examples/backreferences.rb

Defined Under Namespace

Modules: CharSets Classes: BackReferenceGroup, BackReferenceReplacer, BackrefNotFound, BaseRepeater, CharGroup, DotGroup, Error, GroupResult, IllegalSyntaxError, MultiGroup, MultiGroupEnd, OneTimeRepeater, OrGroup, Parser, PlusRepeater, QuestionMarkRepeater, RangeRepeater, ResultCountLimiters, SingleCharGroup, StarRepeater, UnsupportedSyntaxError

Constant Summary collapse

VERSION =
'0.4.1'
BackslashCharMap =

Map of special regex characters, to their associated character sets

{
  'd' => CharSets::Digit,
  'D' => CharSets::Lower | CharSets::Upper | CharSets::Punct,
  'w' => CharSets::Lower | CharSets::Upper | CharSets::Digit | ['_'],
  'W' => CharSets::Punct.reject { |val| val == '_' },
  's' => CharSets::Whitespace,
  'S' => CharSets::Any - CharSets::Whitespace,
  'h' => CharSets::Hex,
  'H' => CharSets::Any - CharSets::Hex,

  't' => ["\t"], # tab
  'n' => ["\n"], # new line
  'r' => ["\r"], # carriage return
  'f' => ["\f"], # form feed
  'a' => ["\a"], # alarm
  'v' => ["\v"], # vertical tab
  'e' => ["\e"], # escape
}

Class Method Summary collapse

Class Method Details

.join_preserving_capture_groups(result) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/regexp-examples/helpers.rb', line 18

def self.join_preserving_capture_groups(result)
  result.flatten!
  subgroups = result
    .map(&:all_subgroups)
    .flatten
  GroupResult.new(result.join, nil, subgroups)
end

.map_results(repeaters) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/regexp-examples/helpers.rb', line 26

def self.map_results(repeaters)
  repeaters
    .map {|repeater| repeater.result}
    .instance_eval do |partial_results|
      RegexpExamples.permutations_of_strings(partial_results)
    end
end

.MaxGroupResultsObject



30
31
32
# File 'lib/regexp-examples/constants.rb', line 30

def self.MaxGroupResults
  ResultCountLimiters.max_group_results
end

.MaxRepeaterVarianceObject



27
28
29
# File 'lib/regexp-examples/constants.rb', line 27

def self.MaxRepeaterVariance
  ResultCountLimiters.max_repeater_variance
end

.permutations_of_strings(arrays_of_strings) ⇒ Object

Given an array of arrays of strings, returns all possible perutations, for strings created by joining one element from each array

For example: permutations_of_strings [ [‘a’], [‘b’], [‘c’, ‘d’, ‘e’] ] #=> [‘abc’, ‘abd’, ‘abe’] permutations_of_strings [ [‘a’, ‘b’], [‘c’, ‘d’] ] #=> [ ‘ac’, ‘ad’, ‘bc’, ‘bd’ ]



10
11
12
13
14
15
16
# File 'lib/regexp-examples/helpers.rb', line 10

def self.permutations_of_strings(arrays_of_strings)
  first = arrays_of_strings.shift
  return first if arrays_of_strings.empty?
  first.product( permutations_of_strings(arrays_of_strings) ).map do |result|
    join_preserving_capture_groups(result)
  end
end