Module: RegexpExamples
- Defined in:
- lib/regexp-examples/parser.rb,
lib/regexp-examples/groups.rb,
lib/regexp-examples/helpers.rb,
lib/regexp-examples/version.rb,
lib/regexp-examples/constants.rb,
lib/regexp-examples/repeaters.rb,
lib/regexp-examples/backreferences.rb,
lib/regexp-examples/chargroup_parser.rb,
lib/regexp-examples/max_results_limiter.rb,
lib/regexp-examples/unicode_char_ranges.rb,
lib/regexp-examples/parser_helpers/parse_group_helper.rb,
lib/regexp-examples/parser_helpers/parse_repeater_helper.rb,
lib/regexp-examples/parser_helpers/charset_negation_helper.rb,
lib/regexp-examples/parser_helpers/parse_multi_group_helper.rb,
lib/regexp-examples/parser_helpers/parse_after_backslash_group_helper.rb
Overview
A common helper used throughout various parser methods
Defined Under Namespace
Modules: CharsetNegationHelper, GroupWithIgnoreCase, ParseAfterBackslashGroupHelper, ParseGroupHelper, ParseMultiGroupHelper, ParseRepeaterHelper, RandomResultBySample Classes: BackReferenceGroup, BackReferenceReplacer, BaseRepeater, CharGroup, ChargroupParser, Config, DotGroup, GroupResult, MaxResultsLimiter, MaxResultsLimiterByProduct, MaxResultsLimiterBySum, MultiGroup, OneTimeRepeater, OrGroup, Parser, PlaceHolderGroup, PlusRepeater, QuestionMarkRepeater, RangeRepeater, SingleCharGroup, StarRepeater, UnicodeCharRanges
Constant Summary collapse
- IllegalSyntaxError =
Class.new(StandardError)
- VERSION =
'1.4.0'.freeze
- BackslashCharMap =
Map of special regex characters, to their associated character sets
{ 'd' => CharSets::Digit, 'D' => CharSets::Any - CharSets::Digit, 'w' => CharSets::Word, 'W' => CharSets::Any - CharSets::Word, '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 }.freeze
- POSIXCharMap =
{ 'alnum' => CharSets::Upper | CharSets::Lower | CharSets::Digit, 'alpha' => CharSets::Upper | CharSets::Lower, 'blank' => [' ', "\t"], 'cntrl' => CharSets::Control, 'digit' => CharSets::Digit, 'graph' => (CharSets::Any - CharSets::Control) - [' '], # Visible chars 'lower' => CharSets::Lower, 'print' => CharSets::Any - CharSets::Control, 'punct' => CharSets::Punct, 'space' => CharSets::Whitespace, 'upper' => CharSets::Upper, 'xdigit' => CharSets::Hex, 'word' => CharSets::Word, 'ascii' => CharSets::Any }.freeze
- NamedPropertyCharMap =
UnicodeCharRanges.new
Class Method Summary collapse
- .generic_map_result(repeaters, method) ⇒ Object
- .join_preserving_capture_groups(result) ⇒ Object
-
.permutations_of_strings(arrays_of_strings, max_results_limiter = MaxResultsLimiterByProduct.new) ⇒ Object
Given an array of arrays of strings, returns all possible perutations for strings, created by joining one element from each array.
Class Method Details
.generic_map_result(repeaters, method) ⇒ Object
36 37 38 39 40 |
# File 'lib/regexp-examples/helpers.rb', line 36 def self.generic_map_result(repeaters, method) permutations_of_strings( repeaters.map { |repeater| repeater.public_send(method) } ) end |
.join_preserving_capture_groups(result) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/regexp-examples/helpers.rb', line 24 def self.join_preserving_capture_groups(result) # Only save the LAST group from repeated capture groups, because # e.g. /([ab]){2} \1/.examples should NOT include "ab a" # (Hence the need for "reverse"!) subgroups = result .flat_map(&:all_subgroups) .reverse .uniq(&:group_id) GroupResult.new(result.join, nil, subgroups) end |
.permutations_of_strings(arrays_of_strings, max_results_limiter = MaxResultsLimiterByProduct.new) ⇒ 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’ ]
Edge case: permutations_of_strings [ [] ] #=> nil (For example, ths occurs during /[^dD]/.examples #=> [])
13 14 15 16 17 18 19 20 21 22 |
# File 'lib/regexp-examples/helpers.rb', line 13 def self.permutations_of_strings(arrays_of_strings, max_results_limiter = MaxResultsLimiterByProduct.new) partial_result = max_results_limiter.limit_results(arrays_of_strings.shift) return partial_result if arrays_of_strings.empty? partial_result.product( permutations_of_strings(arrays_of_strings, max_results_limiter) ).map do |result| join_preserving_capture_groups(result) end end |