regeng 0.2.2 → 0.2.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/changelog.md +2 -0
- data/lib/regeng.rb +23 -3
- data/lib/regeng/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 6ed0b6773d2d8d9fa35aafce95be9e7791a825afb9db98693cfc70cdb20a129a
|
|
4
|
+
data.tar.gz: 13c38505d60b9497796dfdc7068b6f08301ded0c03498af6a0e4e31993a6b7cd
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: '06820b558828006ed09a74f4be4f6382c886bef6696cf1037d31cb61dbe03833a76568e7f68a50cc358633f4c4c38cc86cf7acea336a9f23f42ba8e375ac91ab'
|
|
7
|
+
data.tar.gz: 4e7d195e367f54d0b67baf20bd094a7dfe2f8ca39b1196cfdc775de98ae4a4178e37500e6489d2d0772f7c4a97314d16119f68ad863ad73400a46fbede91d314
|
data/Gemfile.lock
CHANGED
data/changelog.md
CHANGED
data/lib/regeng.rb
CHANGED
|
@@ -4,8 +4,6 @@ require 'regeng/version'
|
|
|
4
4
|
|
|
5
5
|
# A gem which creates regular expressions using plain english.
|
|
6
6
|
module Regeng
|
|
7
|
-
class Error < StandardError; end
|
|
8
|
-
|
|
9
7
|
CHARACTER_COND = /((any )?(character)(s)?( except)?( between)?( [a-zA-Z])+((-)|( through )|( to )|( and )){1}[a-zA-Z]){1}/.freeze
|
|
10
8
|
CHARACTER_SIMP = /((any )?((uppercase )?|(lowercase )?)(character)(s)?){1}/.freeze
|
|
11
9
|
|
|
@@ -14,12 +12,20 @@ module Regeng
|
|
|
14
12
|
|
|
15
13
|
AT_COND = /( at )((start)|(end))( of )((line)|(string))/.freeze
|
|
16
14
|
|
|
15
|
+
# Regeng's error raiser.
|
|
16
|
+
class Error < StandardError
|
|
17
|
+
def self.invalid_expression(string)
|
|
18
|
+
raise self, "Invalid expression: (#{string})"
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
# Returns a new regular expression using plain english.
|
|
17
23
|
def self.new(string)
|
|
18
24
|
expression(string)
|
|
19
25
|
end
|
|
20
26
|
|
|
27
|
+
# Processes plain english into a regular expression.
|
|
21
28
|
def self.expression(string)
|
|
22
|
-
expression = ''
|
|
23
29
|
if CHARACTER_COND.match?(string)
|
|
24
30
|
expression = characters_condition(string)
|
|
25
31
|
elsif CHARACTER_SIMP.match?(string)
|
|
@@ -30,11 +36,15 @@ module Regeng
|
|
|
30
36
|
expression = digit_simple(string)
|
|
31
37
|
end
|
|
32
38
|
|
|
39
|
+
# Adds a 'at' modifier if one is present.
|
|
33
40
|
at_mod = at_condition(string) if AT_COND.match?(string)
|
|
34
41
|
|
|
42
|
+
Error.invalid_expression if expression.nil?
|
|
43
|
+
|
|
35
44
|
Regexp.new "#{at_mod}#{expression}"
|
|
36
45
|
end
|
|
37
46
|
|
|
47
|
+
# Processes conditional character expressions.
|
|
38
48
|
def self.characters_condition(string)
|
|
39
49
|
except = '^' if /(except)/.match?(string)
|
|
40
50
|
multiples = '+' if /(character)(s)/.match?(string)
|
|
@@ -50,9 +60,13 @@ module Regeng
|
|
|
50
60
|
unfiltered_mod = string.match(/( ([a-z] )+(and )([a-z]))/)
|
|
51
61
|
character_mod = unfiltered_mod.to_s.gsub(/( )|(and )/, '')
|
|
52
62
|
end
|
|
63
|
+
|
|
64
|
+
Error.invalid_expression if character_mod.nil? || character_mod == '-'
|
|
65
|
+
|
|
53
66
|
"[#{except}#{character_mod}]#{multiples}"
|
|
54
67
|
end
|
|
55
68
|
|
|
69
|
+
# Processes simple character expressions.
|
|
56
70
|
def self.characters_simple(string)
|
|
57
71
|
character_mod = 'a-zA-Z'
|
|
58
72
|
multiples = '+' if /(character)(s)/.match?(string)
|
|
@@ -64,6 +78,7 @@ module Regeng
|
|
|
64
78
|
"[#{character_mod}]#{multiples}"
|
|
65
79
|
end
|
|
66
80
|
|
|
81
|
+
# Processes conditional digit/number expressions.
|
|
67
82
|
def self.digit_condition(string)
|
|
68
83
|
except = '^' if /(except)/.match?(string)
|
|
69
84
|
multiples = '+' if /((digit)|(number))(s)/.match?(string)
|
|
@@ -79,15 +94,20 @@ module Regeng
|
|
|
79
94
|
unfiltered_mod = string.match(/( ([0-9] )+(and )([0-9]))/)
|
|
80
95
|
digit_mod = unfiltered_mod.to_s.gsub(/( )|(and )/, '')
|
|
81
96
|
end
|
|
97
|
+
|
|
98
|
+
Error.invalid_expression if digit_mod.nil? || digit_mod == '-'
|
|
99
|
+
|
|
82
100
|
"[#{except}#{digit_mod}]#{multiples}"
|
|
83
101
|
end
|
|
84
102
|
|
|
103
|
+
# Processes simple digit expressions.
|
|
85
104
|
def self.digit_simple(string)
|
|
86
105
|
digit_mod = '0-9'
|
|
87
106
|
multiples = '+' if /((digit)|(number))(s)/.match?(string)
|
|
88
107
|
"[#{digit_mod}]#{multiples}"
|
|
89
108
|
end
|
|
90
109
|
|
|
110
|
+
# Processes 'at end/start of' expression modifier.
|
|
91
111
|
def self.at_condition(string)
|
|
92
112
|
at_mod = '^' if /(start of line)/.match?(string)
|
|
93
113
|
at_mod = '$' if /(end of line)/.match?(string)
|
data/lib/regeng/version.rb
CHANGED