Module: DemoLorem::Helpers::RegexHelper

Included in:
Generator
Defined in:
lib/demo_lorem/helpers/regex_helper.rb

Overview

RegexHelper module generates random regular expressions

Instance Method Summary collapse

Instance Method Details

#random_regexObject



7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/demo_lorem/helpers/regex_helper.rb', line 7

def random_regex
  characters = ('a'..'z').to_a + ('A'..'Z').to_a + ('0'..'9').to_a
  metacharacters = %w[. * + ?]
  quantifiers = %w[{1,3} {2,5} + *]
  character_classes = ['[a-z]', '[A-Z]', '[0-9]', '\w', '\d']
  base_pattern = characters.sample(5).join
  base_pattern = "#{character_classes.sample}#{base_pattern}"
  base_pattern = "#{base_pattern}#{metacharacters.sample}#{quantifiers.sample}"
  full_pattern = "(#{base_pattern})"

  Regexp.new(full_pattern)
end