simple_regex 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 4cdb3c7de307a3117543bbe93423f34f4386206c
4
+ data.tar.gz: 5d01fde5da692cbae69d8385b02dfe76d4fe9b3b
5
+ SHA512:
6
+ metadata.gz: 1364a4dc2c303d1a1f7b258062e1d0ab504fa2d83fdb78de5a52d1ecf6805c377620d1f28500c64ea47161065f8aa12bdee243634db2183f213ccd22f730ac25
7
+ data.tar.gz: 5e82e7bc9f76de2cddf23e8338471f4d5b69083d75ea58a12e5082ed6493aaa2bd193f596665c8c25cd0d4d042f7f78b6833fb67784b256168afe8514ccb3c12
@@ -0,0 +1,155 @@
1
+ class SimpleRegex
2
+
3
+ class Error < StandardError; end
4
+
5
+ PATTERNS = {
6
+ 'digit' => '[0-9]',
7
+ 'lowercase' => '[a-z]',
8
+ 'uppercase' => '[A-Z]',
9
+ 'letter' => '[A-Za-z]',
10
+ 'alphanumeric' => '[A-Za-z0-9]',
11
+ 'whitespace' => '\s',
12
+ 'space' => ' ',
13
+ 'tab' => '\t'
14
+ }
15
+
16
+ ESCAPED_CHARS = %w(
17
+ * . ? ^ + $ | ( ) [ ] { }
18
+ )
19
+
20
+ def initialize
21
+ @str = ''
22
+ @ended = false
23
+ end
24
+
25
+ def start_with(*args)
26
+ raise SimpleRegex::Error.new('#start_with? called multiple times') unless @str.empty?
27
+ write '^%s', args
28
+ end
29
+
30
+ def append(*args)
31
+ write interpret(*args)
32
+ end
33
+ alias_method :then, :append
34
+
35
+ def end_with(*args)
36
+ write '%s$', args
37
+ @ended = true
38
+ self
39
+ end
40
+
41
+ def maybe(*args)
42
+ write '%s?', args
43
+ end
44
+
45
+ def not(*args)
46
+ write '(?!%s)', args
47
+ end
48
+
49
+ def one_of(ary)
50
+ write '[%s]' % ary.map { |c| escape(c) }.join('|')
51
+ end
52
+
53
+ def between(range, pattern)
54
+ unless range.length == 2 && range.any? { |i| i.is_a?(Integer) }
55
+ raise SimpleRegex:Error.new('must provide an array of 2 elements, one of them must be an integer')
56
+ end
57
+
58
+ write '%s{%s,%s}' % [interpret(pattern), range[0], range[1]]
59
+ end
60
+
61
+ def at_least(times, pattern)
62
+ between [times, nil], pattern
63
+ end
64
+
65
+ def at_most(times, pattern)
66
+ between [nil, times], pattern
67
+ end
68
+
69
+ def zero_or_more(pattern)
70
+ write '%s*', pattern
71
+ end
72
+
73
+ def one_or_more(pattern)
74
+ write '%s+', pattern
75
+ end
76
+
77
+ def regex
78
+ Regexp.new(@str)
79
+ end
80
+ alias_method :get, :regex
81
+
82
+ def =~(other)
83
+ regex =~ other
84
+ end
85
+
86
+ def method_missing(meth, *args, &block)
87
+ regex.send(meth, *args, &block)
88
+ end
89
+
90
+ def respond_to?(meth)
91
+ regex.respond_to?(meth) || super
92
+ end
93
+
94
+ def to_s
95
+ "#<SimpleRegex:#{object_id} regex=/#{@str}/>"
96
+ end
97
+
98
+ def inspect
99
+ to_s
100
+ end
101
+
102
+ private
103
+
104
+ def write(str, args=nil)
105
+ raise SimpleRegex::Error.new('#end_with has already been called') if @ended
106
+ @str << (args.nil? ? str : str % interpret(*args))
107
+ self
108
+ end
109
+
110
+ # Translate/escape characters etc and return regex-ready string
111
+ def interpret(*args)
112
+ case args.length
113
+ when 2 then numbered_constraint(*args)
114
+ when 1 then patterned_constraint(*args)
115
+ else raise ArgumentError
116
+ end
117
+ end
118
+
119
+ # Ex: (2, 'x') or (3, :digits)
120
+ def numbered_constraint(count, type)
121
+ pattern = patterned_constraint(type)
122
+ raise SimpleRegex::Error.new('Unrecognized pattern') if pattern.nil? || pattern.empty?
123
+ '%s{%s}' % [pattern, count]
124
+ end
125
+
126
+ # Ex: ('aa') or ('$')
127
+ def patterned_constraint(pattern)
128
+ escape translate(pattern)
129
+ end
130
+
131
+ # Remove a trailing 's', if there is one
132
+ def singularize(word)
133
+ str = word.to_s
134
+ str.end_with?('s') ? str[0..-2] : str
135
+ end
136
+
137
+ # Escape special regex characters in a string
138
+ #
139
+ # Ex:
140
+ # escape("one.two")
141
+ # # => "one\.two"
142
+ #
143
+ def escape(pattern)
144
+ pattern.to_s.gsub(/.+/) do |char|
145
+ ESCAPED_CHARS.include?(char) ? "\\#{char}" : char
146
+ end
147
+ end
148
+
149
+ # Translate an identifier such as :digits to [0-9], etc
150
+ # Returns the original identifier if no character class found
151
+ def translate(pattern)
152
+ PATTERNS[singularize(pattern)] || pattern
153
+ end
154
+
155
+ end
@@ -0,0 +1,3 @@
1
+ class SimpleRegex
2
+ VERSION = '0.0.1'
3
+ end
metadata ADDED
@@ -0,0 +1,63 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: simple_regex
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Andrew Berls
8
+ - Puru Dahal
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-09-13 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - "~>"
19
+ - !ruby/object:Gem::Version
20
+ version: '3.6'
21
+ type: :development
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - "~>"
26
+ - !ruby/object:Gem::Version
27
+ version: '3.6'
28
+ description: " SimpleRegex provides a flexible syntax for constructing regular expressions
29
+ by chaining Ruby method calls instead of deciphering cryptic syntax. "
30
+ email:
31
+ - andrew.berls@gmail.com
32
+ - puru@dahal.me
33
+ executables: []
34
+ extensions: []
35
+ extra_rdoc_files: []
36
+ files:
37
+ - lib/simple_regex.rb
38
+ - lib/simple_regex/version.rb
39
+ homepage: https://github.com/superpowr/simple_regex
40
+ licenses:
41
+ - MIT
42
+ metadata: {}
43
+ post_install_message:
44
+ rdoc_options: []
45
+ require_paths:
46
+ - lib
47
+ required_ruby_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: '0'
52
+ required_rubygems_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: '0'
57
+ requirements: []
58
+ rubyforge_project:
59
+ rubygems_version: 2.6.13
60
+ signing_key:
61
+ specification_version: 4
62
+ summary: Simple Regular Expression
63
+ test_files: []