给出以下2个字符串 -
select * from abc;
select * from abc where;
我希望正则表达式匹配任何东西 -
from
之后;
。from
和where
之间。以下2个正则表达式适用于上述2个案例 -
(re-find #"(?<=from |FROM ).*(?= where| WHERE)" "select * from abc where") => abc
(re-find #"(?<=from |FROM ).*(?=;)" "Select * from abc;") => abc
如何将上述2个正则表达式组合用于上述两个字符串?
我尝试了以下但是这不起作用 -
(re-find #"(?<=from |FROM ).*(?= where| WHERE)|(?=;) " "select * from abc;") => nil
编辑 -
如何使正则表达式适用于以下字符串 -
select * from abc \n where;
select * from abc \n \n;
语句始终使用;
终止。
但是之前可能会有空格或换行符。
答案 0 :(得分:2)
只需在前瞻中包含<select id="productextra[@count@]">
<option value="[@option_id@]">Yes</option>
<option value="[@option_id@]">No</option>
</select>
。
;
答案 1 :(得分:2)
您可以按如下方式修改正则表达式。 (?i)
修饰符用于不区分大小写的匹配:
user> (re-find #"(?i)(?<=from ).*?(?= where|;)" "SELECT * FROM abc WHERE")
"abc"
user> (re-find #"(?i)(?<=from ).*?(?= where|;)" "Select * from abc;")
"abc"
更新:您可以加入s
修饰符,以强制.
跨越换行符。
(re-find #"(?si)(?<=from ).*?(?= where|;)" "select * from abc \n where;")
答案 2 :(得分:1)
答案 3 :(得分:1)
>>> s
'select * from abc;'
>>> s2
'select * from abc where;'
>>> s3
'select * from abc WHERE;'
>>> s4
'select * from abc \n where;'
>>> s5
'select * from abc \n \n;'
>>> s6
'select * from efg \n \n;'
>>> s7
'select * from efg where \n \n;'
>>> for i in s, s2, s3, s4, s5, s6, s7:
... re.search(r'from (\S+)(?:\s+)?(?=;|where)', i, flags=re.I).groups()
...
('abc',)
('abc',)
('abc',)
('abc',)
('abc',)
('efg',)
('efg',)
>>>