-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDerive Regular Expression from string pattern.ruby
More file actions
263 lines (229 loc) · 5.79 KB
/
Derive Regular Expression from string pattern.ruby
File metadata and controls
263 lines (229 loc) · 5.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
# https://stackoverflow.com/questions/3135575/programmatically-derive-a-regular-expression-from-a-string
# by Joc
LOOP_COUNT = 100
class Attempt
# let's try email
HITS = %w[j@j.com j@j.co.uk gates@microsoft.com sales@microsoft.com sjobs@apple.com sales@apple.com frddy@aol.com thing1@charity.org sales@mybad.org.uk thing.one@drseuss.com]
MISSES = %w[j@j j@j@.com j.com @domain.com nochance eric@google. eric@google.com. username-at-domain-dot-com linux.org eff.org microsoft.com sjobs.apple.com www.apple.com]
# odd mixture of numbers and letters, designed to confuse
# HITS = %w[a123 a999 a600 a545 a100 b001 b847 a928 c203]
# MISSES = %w[abc def ghi jkl mno pqr stu vwx xyz h234 k987]
# consonants versus vowels
# HITS = %w[bcd cdb fgh ghf jkl klj mnp npm qrs srq tvw vwt xzb bzx]
# MISSES = %w[aei oyu oio euu uio ioe aee ooo]
# letters < 11 chars and no numbers
# HITS = %w[aaa aaaa abaa azaz monkey longstring stringlong]
# MISSES = %w[aa aa1 aa0 b9b 6zz longstringz m_m ff5 666 anotherlongstring]
MAX_SUCCESSES = HITS.size + MISSES.size
# Setup the various Regular Expression operators, etc..
RELEMENTS = %w[. ? * + ( ) \[ \] - | ^ $ \\ : @ / { }]
%w[A b B d D S s W w z Z].each do |chr|
RELEMENTS << "\\#{chr}"
end
%w[alnum alpha blank cntrl digit lower print punct space upper xdigit].each do |special|
RELEMENTS << "[:#{special}:]"
end
('a'..'z').each do |chr|
RELEMENTS << chr
end
('A'..'Z').each do |chr|
RELEMENTS << chr
end
(0..9).each do |chr|
RELEMENTS << chr.to_s
end
START_SIZE = 8
attr_accessor :operators, :successes
def initialize(ary = [])
@operators = ary
if ary.length < 1
START_SIZE.times do
@operators << random_op
end
end
@score = 0
@decay = 1
make_regexp
end
def make_regexp
begin
@regexp = Regexp.new( @operators.join("") )
rescue
# "INVALID Regexp"
@regexp = nil
@score = -1000
end
end
def random_op
RELEMENTS[rand(RELEMENTS.size)]
end
def decay
@decay -= 1
end
def test
@successes = 0
if @regexp
HITS.each do |hit|
result = (hit =~ @regexp)
if result != nil
reward
end
end
MISSES.each do |miss|
result = (miss =~ @regexp)
if result == nil
reward
end
end
end
@score = @successes
self
end
def reward
@successes += 1
end
def cross other
len = size
olen = other.size
split = rand(len)
ops = []
@operators.length.times do |num|
if num < split
ops << @operators[num]
else
ops << other.operators[num + (olen - len)]
end
end
Attempt.new ops
end
# apply a random mutation, you don't have to use all of them
def mutate
send [:flip, :add_rand, :add_first, :add_last, :sub_rand, :sub_first, :sub_last, :swap][rand(8)]
make_regexp
self
end
## mutate methods
def flip
@operators[rand(size)] = random_op
end
def add_rand
@operators.insert rand(size), random_op
end
def add_first
@operators.insert 0, random_op
end
def add_last
@operators << random_op
end
def sub_rand
@operators.delete_at rand(size)
end
def sub_first
@operators.delete_at 0
end
def sub_last
@operators.delete_at size
end
def swap
to = rand(size)
begin
from = rand(size)
end while to == from
@operators[to], @operators[from] = @operators[from], @operators[to]
end
def regexp_to_s
@operators.join("")
end
def <=> other
score <=> other.score
end
def size
@operators.length
end
def to_s
"#{regexp_to_s} #{score}"
end
def dup
Attempt.new @operators.dup
end
def score
if @score > 0
ret = case
when (size > START_SIZE * 2)
@score-20
when size > START_SIZE
@score-2
else
@score #+ START_SIZE - size
end
ret + @decay
else
@score + @decay
end
end
def == other
to_s == other.to_s
end
def stats
puts "Regexp #{@regexp.inspect}"
puts "Length #{@operators.length}"
puts "Successes #{@successes}/#{MAX_SUCCESSES}"
puts "HITS"
HITS.each do |hit|
result = (hit =~ @regexp)
if result == nil
puts "\tFAIL #{hit}"
else
puts "\tOK #{hit} #{result}"
end
end
puts "MISSES"
MISSES.each do |miss|
result = (miss =~ @regexp)
if result == nil
puts "\tOK #{miss}"
else
puts "\tFAIL #{miss} #{result}"
end
end
end
end
$stderr.reopen("/dev/null", "w") # turn off stderr to stop streams of bad rexexp messages
# find some seed attempt values
results = []
10000.times do
a = Attempt.new
a.test
if a.score > 0
# puts "#{a.regexp_to_s} #{a.score}"
results << a
end
end
results.sort!.reverse!
puts "SEED ATTEMPTS"
puts results[0..9]
old_result = nil
LOOP_COUNT.times do |i|
results = results[0..9]
results.map {|r| r.decay }
3.times do
new_results = results.map {|r| r.dup.mutate.test}
results.concat new_results
new_results = results.map {|r| r.cross( results[rand(10)] ).test }
results.concat new_results
end
new_results = []
20.times do
new_results << Attempt.new.test
end
results.concat new_results
results.sort!.reverse!
if old_result != results[0].score
old_result = results[0].score
end
puts "#{i} #{results[0]}"
end
puts "\n--------------------------------------------------"
puts "Winner! #{results[0]}"
puts "--------------------------------------------------\n"
results[0].stats