Class: RegXing::Regex

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(exp) ⇒ Regex

Returns a new instance of Regex.



89
90
91
# File 'lib/regxing/regex.rb', line 89

def initialize(exp)
  @expression = exp
end

Instance Attribute Details

#expressionObject (readonly)

Instance Methods




87
88
89
# File 'lib/regxing/regex.rb', line 87

def expression
  @expression
end

Class Method Details

.count_indicatorsObject



33
34
35
# File 'lib/regxing/regex.rb', line 33

def count_indicators
  [ /(?<!\\)\*/, /(?<!\\)\+/, /(?<!\\)\?/, /\{\d*\,?\d*\}/ ]
end

.matchersObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/regxing/regex.rb', line 8

def matchers
  {
    /(?<!\\)\./          => random_letter,
    /\\d/                => random_number,
    /\\w/                => random_letter,
    /\\W/                => random_non_word_character,
    /\\D/                => random_letter,
    /\\h/                => random_hexdigit_character,
    /\\H/                => random_non_hexdigit_character,
    /\\s/                => " ",
    /\\S/                => random_letter,
    /\[\[\:alnum\:\]\]/  => random_letter,
    /\[\[\:alpha\:\]\]/  => random_letter,
    /\[\[\:digit\:\]\]/  => random_number,
    /\[\[\:graph\:\]\]/  => random_letter,
    /\[\[\:lower\:\]\]/  => random_lowercase_letter,
    /\[\[\:print\:\]\]/  => random_letter,
    /\[\[\:xdigit\:\]\]/ => random_hexdigit_character,
    /\[\[\:punct\:\]\]/  => random_non_word_character,
    /\[\[\:space\:\]\]/  => " ",
    /\[\[\:cntrl\:\]\]/  => "\a",
    /\[\[\:upper\:\]\]/  => random_uppercase_letter
  }
end

.process_count_indicator(indicator) ⇒ Object



37
38
39
40
41
42
43
44
45
46
# File 'lib/regxing/regex.rb', line 37

def process_count_indicator(indicator)
  if indicator.match count_indicators.last

    minimum = indicator.match(/(?<=\{)\d+/) ? indicator.match(/(?<=\{)\d+/)[0].to_i : 1

    return minimum
  else
    return 1
  end
end

Instance Method Details

#splitObject



97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/regxing/regex.rb', line 97

def split
  arr = to_s.scan(/\\\?|[^\\]?\?|\\\.|[^\\]?\.|\\\+|[^\\]?\+|\\\*|[^\\]?\*|\\[a-zA-Z]|(?<!\\)[a-zA-Z]|\{\d*\,?\d*\}|\[\[\:.{5,6}\:\]\]|./).flatten

  arr.each_with_index do |item, index|
    if RegXing::Regex.count_indicators.any? {|exp| arr[index + 1] && arr[index + 1].match(exp) }
      arr[index] = [ item, RegXing::Regex.process_count_indicator(arr.delete_at(index + 1)) ]
    else
      arr[index] = [item, 1]
    end
  end

  arr
end

#to_sObject



93
94
95
# File 'lib/regxing/regex.rb', line 93

def to_s
  expression.inspect[1..-2]
end