A basic version of this problem is using an regex to translate something like abc like '%FFddsdE%'
into LOWER(abc) like '%ffddsde%'
and the following code works well
JavaScript
x
13
13
1
import re
2
text = "abc like '%FFddsdE%'"
3
print("before: " + text)
4
text = re.sub('([^ ^n]+?) ?like ?'([^ ]+)'',
5
lambda s: f'LOWER({s.group(1)}) like '{s.group(2).lower()}'',
6
text, flags=re.I)
7
print("after: " + text)
8
9
10
#the output is
11
#before: abc like '%FFddsdE%'
12
#after: LOWER(abc) like '%ffddsde%'
13
However, everything crash down if we want to expand this problem a little bit, to both like
and not like
JavaScript
1
16
16
1
import re
2
3
text = "abc not like '%FFddsdE%' and bcd like '%XyZ%'"
4
print("before: " + text)
5
text = re.sub('([^ ^n]+?) ?not like ?'([^ ]+)'',
6
lambda s: f'LOWER({s.group(1)}) not like '{s.group(2).lower()}'',
7
text, flags=re.I)
8
text = re.sub('([^ ^n]+?) ?like ?'([^ ]+)'',
9
lambda s: f'LOWER({s.group(1)}) like '{s.group(2).lower()}'',
10
text, flags=re.I)
11
print("after: " + text)
12
13
#the output is
14
#before: abc not like '%FFddsdE%' and bcd like '%XyZ%'
15
#after: LOWER(abc) LOWER(not) like '%ffddsde%' and LOWER(bcd) like '%xyz%'
16
It gives LOWER(not)
in the output.
I am using python 3 btw. Thanks for any help!
Advertisement
Answer
You could use another group to match like
and not like
at the same time.
JavaScript
1
11
11
1
import re
2
text = "abc not like '%FFddsdE%' and bcd like '%XyZ%'"
3
print("before: " + text)
4
text = re.sub('([^ ^n]+?) ?(like|not like) ?'([^ ]+)'',
5
lambda s: f'LOWER({s.group(1)}) {s.group(2)} '{s.group(3).lower()}'',
6
text, flags=re.I)
7
print("after: " + text)
8
9
# before: abc not like '%FFddsdE%' and bcd like '%XyZ%'
10
# after: LOWER(abc) not like '%ffddsde%' and LOWER(bcd) like '%xyz%'
11