Regex 在正则表达式中的两个组合子句之间交替使用
考虑到以下两个条件:Regex 在正则表达式中的两个组合子句之间交替使用,regex,clojure,Regex,Clojure,考虑到以下两个条件: select * from abc; select * from abc where; select * from abc \n where; select * from abc \n \n; 我想要正则表达式匹配任何东西- 从到之后的 介于from和where之间 以下2个正则表达式适用于上述2种情况- (re-find #"(?<=from |FROM ).*(?= where| WHERE)" "select * from abc where") =
select * from abc;
select * from abc where;
select * from abc \n where;
select * from abc \n \n;
我想要正则表达式匹配任何东西-
代码>
from
和where
之间(re-find #"(?<=from |FROM ).*(?= where| WHERE)" "select * from abc where") => abc
(re-find #"(?<=from |FROM ).*(?=;)" "Select * from abc;") => abc
语句总是使用终止代码>
但是,前面可能有空格或换行符。只包括代码>在前瞻中
"(?<=from |FROM )[\s\S]*?(?= where| WHERE|;)"
”(?您可以按如下方式修改正则表达式。(?i)
修饰符用于不区分大小写的匹配:
user> (re-find #"(?i)(?<=from ).*?(?= where|;)" "SELECT * FROM abc WHERE")
"abc"
user> (re-find #"(?i)(?<=from ).*?(?= where|;)" "Select * from abc;")
"abc"
(?WHERE(大写)怎么办?(?i)
修饰符会处理这个问题。同样在匹配语句“*?”中,“?”的作用是什么。我看到没有它,正则表达式就不能工作。(我知道?的作用通常是选择前面模式的0或1个元素)。s修饰符可以工作,但是它也选择了\n,如何排除它?@murtaza52,您可以使用(?si)(?您正在运行的语言?不适用于所有字符。为什么我需要使用\s指定空格和换行符?点匹配除换行符以外的所有字符(换行符或\r
)除非未指定点所有修改器(?s)
user> (re-find #"(?i)(?<=from ).*?(?= where|;)" "SELECT * FROM abc WHERE")
"abc"
user> (re-find #"(?i)(?<=from ).*?(?= where|;)" "Select * from abc;")
"abc"
(re-find #"(?si)(?<=from ).*?(?= where|;)" "select * from abc \n where;")
(?<=from )([\s\S]*?)(?= where|;)
>>> 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',)
>>>