forked from Wizcorp/libbash
-
Notifications
You must be signed in to change notification settings - Fork 1
/
regex.sh
319 lines (268 loc) · 10.4 KB
/
regex.sh
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
#!/bin/bash
##
# title : regex.sh
# description : This script provides helper functions for
# creating or manipulating regular expressions.
# author : Almir Kadric
# created on : 2012-11-22
# version : 0.1
##
##
# Takes in a comma separated list of numeric reanges which can be
# either a single number or a range <from>-<to>. It will then
# generate a regular expression which will match those ranges.
#
# Usage: RangeToRegex "<ranges>"
#
# Arguments:
# ranges: comma separated list of numeric ranges. i.e. "1,2,5,10-20,100-200"
##
function RangeToRegex() {
# Initial regex to start multi selections
regex="";
# Function to add 'OR' if required
function addOr() {
if [ "${regex}" != "" ]
then
regex+="|";
fi
}
# Run parsing loop
for piece in $(echo ${1} | sed 's/,/ /g')
do
# separate from and to in case piece is a range
from=$(echo ${piece} | cut -d '-' -f 1);
to=$(echo ${piece} | cut -d '-' -f 2);
#echo "${piece}:";
# Attempt to parse single number, range or fail
if [ "$(echo ${piece} | grep '^[0-9][0-9]*$')" != "" ]
then
# piece is a number
addOr;
regex+="${piece}";
elif [ "$(echo ${piece} | grep '^[0-9][0-9]*-[0-9][0-9]*$')" != "" ] && [ "${from}" = "${to}" ]
then
# piece is a range of the same value
addOr;
regex+="${from}";
elif [ "$(echo ${piece} | grep '^[0-9][0-9]*-[0-9][0-9]*$')" != "" ] && [ "${from}" != "${to}" ]
then
# Sort from and to and get largest character length
if [ $from -gt $to ]
then
tmp="$from";
from="$to";
to="$tmp";
fi
length=${#to};
# Create from digit arrays and add leading zeros
fromArr=();
for digit in $(echo ${from} | sed 's/\([0-9]\)/\ \1/g')
do
fromArr+=("${digit}");
done
# Create to digit arrays and add leading zeros
toArr=();
for digit in $(echo ${to} | sed 's/\([0-9]\)/\ \1/g')
do
toArr+=("${digit}");
done
# Generate largest 10(power) regex
for i in $(seq $(( ${#to} )))
do
p=$(( ${#to} - $i ));
#
if [ ${#from} -eq ${#to} ] && [ "${from:0:$p}" = "${to:0:$p}" ]
then
continue;
# if (last digit) and (not equal to 0)
elif [ $i -eq 1 ] && [ ${toArr[$p]} -ne 0 ]
then
addOr;
regex+="${to:0:$p}";
regex+="[0-${toArr[$p]}]";
# if (last digit) and (is equal to 0)
elif [ $i -eq 1 ] && [ ${toArr[$p]} -eq 0 ]
then
addOr;
regex+="${to:0:$p}";
regex+="0";
# if (not last digit) and (not first digit) and (is greater than 1)
elif [ $i -gt 1 ] && [ $p -ne 0 ] && [ ${toArr[$p]} -gt 1 ]
then
addOr;
regex+="${to:0:$p}";
regex+="[0-$(( ${toArr[$p]} - 1 ))]";
for j in $(seq $(( $i - 1 )) )
do
regex+="[0-9]";
done
# if (not last digit) and (not first digit) and (equal to 1)
elif [ $i -gt 1 ] && [ $p -ne 0 ] && [ ${toArr[$p]} -eq 1 ]
then
addOr;
regex+="${to:0:$p}";
regex+="0";
for j in $(seq $(( $i - 1 )) )
do
regex+="[0-9]";
done
# if (not last digit) and (not first digit) and (equal to 0)
elif [ $i -gt 1 ] && [ $p -ne 0 ] && [ ${toArr[$p]} -eq 0 ]
then
# Redundant 10(power)
continue;
# if (not last digit) and (first digit) and (greater than 2)
elif [ $i -gt 1 ] && [ $p -eq 0 ] && [ ${toArr[$p]} -gt 2 ]
then
addOr;
regex+="[1-$(( ${toArr[$p]} - 1 ))]";
for j in $(seq $(( $i - 1 )) )
do
regex+="[0-9]";
done
# if (not last digit) and (first digit) and (equal to 2)
elif [ $i -gt 1 ] && [ $p -eq 0 ] && [ ${toArr[$p]} -eq 2 ]
then
addOr;
regex+="1";
for j in $(seq $(( $i - 1 )) )
do
regex+="[0-9]";
done
# if (not last digit) and (first digit) and (equal to 1)
elif [ $i -gt 1 ] && [ $p -eq 0 ] && [ ${toArr[$p]} -eq 1 ]
then
# Redundant
continue;
# otherwise un-handled case
else
echo "Unhandled exception: Unexpected case in largest power parsing." 1>&2;
return 1;
fi
done
# Generate 10(powers) between lowest and highest
if [ ${#from} -ne ${#to} ]
then
# Generate missing 10(powers)
for i in $(seq $(( ${#from} + 1 )) $(( ${#to} - 1 )))
do
# Generate full range regext for power
addOr;
regex+="[1-9]";
for j in $(seq $(( $i - 1 )))
do
regex+="[0-9]";
done
done
else
# Fill in 10 powers between from and to
for i in $(seq ${#from})
do
p=$(( ${#from} - $i + 1 ));
# if (substring of to and from is equal) and (difference between following character is greater than 2)
if [ "${from:0:$p}" = "${to:0:$p}" ] && [ $(( ${toArr[$p]} - ${fromArr[$p]} )) -gt 2 ]
then
addOr;
regex+="${from:0:$p}";
regex+="[$(( ${fromArr[$p]} + 1 ))-$(( ${toArr[$p]} - 1 ))]";
for i in $(seq $(( ${#from} - $p - 1)))
do
regex+="[0-9]";
done
# if (substring of to and from is equal) and (difference between following character is equal to 2)
elif [ "${from:0:$p}" = "${to:0:$p}" ] && [ $(( ${toArr[$p]} - ${fromArr[$p]} )) -eq 2 ]
then
addOr;
regex+="${from:0:$p}";
regex+="$(( ${fromArr[$p]} + 1 ))";
for i in $(seq $(( ${#from} - $p - 1)))
do
regex+="[0-9]";
done
fi
done
fi
# Generate lowest 10(power) regex
lastDigit=$(( ${#from} - 1 ));
# if to and from in same 10 power range
if [ $(( ${fromArr[$lastDigit]} - 9 )) -ne 0 ] && [ "${from:0:$(( ${#from} - 1 ))}" = "${to:0:$(( ${#to} - 1 ))}" ]
then
addOr;
regex+="${from:0:$(( ${#from} - 1 ))}";
regex+="[${fromArr[$lastDigit]}-${toArr[$lastDigit]}]";
# if (last digit not equal 9) and (from is 100 and above)
elif [ $(( ${fromArr[$lastDigit]} - 9 )) -ne 0 ] && [ ${#from} -gt 2 ]
then
addOr;
regex+="${from:0:$(( ${#from} - 1 ))}";
regex+="[${fromArr[$lastDigit]}-9]";
# if (last digit not equal 9) and (from is the 10s power)
elif [ $(( ${fromArr[$lastDigit]} - 9 )) -ne 0 ] && [ ${#from} -eq 2 ]
then
addOr;
regex+="${from:0:1}";
regex+="[${fromArr[$lastDigit]}-9]";
# if (last digit not equal 9) and (from is less than 10)
elif [ $(( ${fromArr[$lastDigit]} - 9 )) -ne 0 ] && [ ${#from} -eq 1 ]
then
addOr;
regex+="[${fromArr[$lastDigit]}-9]";
# if (last digit equal to 9)
elif [ $(( ${fromArr[$lastDigit]} - 9 )) -eq 0 ]
then
addOr;
regex+="${from}";
# otherwise un-handled case
else
echo "Unhandled exception: Unexpected case in lowest power parsing." 1>&2;
return 1;
fi
# Complete lowest power
for i in $(seq $(( ${#from} - 1 )))
do
p=$(( ${#from} - $i - 1 ));
# if (from and 2 in same power) and (from and to leading substring is equal)
if [ ${#from} -eq ${#to} ] && [ "${from:0:$p}" = "${to:0:$p}" ]
then
continue;
# if (digit is less than 8)
elif [ ${fromArr[$p]} -lt 8 ]
then
addOr;
regex+="${from:0:$p}";
regex+="[$(( ${fromArr[$p]} + 1 ))-9]";
for j in $(seq $(( $i )) )
do
regex+="[0-9]";
done
# if (digit is equal to 8)
elif [ ${fromArr[$p]} -eq 8 ]
then
addOr;
regex+="${from:0:$p}";
regex+="9";
for j in $(seq $i)
do
regex+="[0-9]";
done
# if (digit is equal to 9)
elif [ ${fromArr[$p]} -eq 9 ]
then
continue;
# otherwise un-handled case
else
echo "Unhandled exception: Unexpected case in lowest power parsing." 1>&2;
return 1;
fi
done
else
# piece is unkNown, fail
echo "Unkown value (${piece}) in range:" 1>&2;
echo "${1}" 1>&2;
return 1;
fi
done
# Echo regex
echo "$(echo ${regex})";
}