我正在使用Python。 请帮我找到正则表达式:
SELECT
SELECT select1
FROM
SELECT A
FROM B
WHERE C
WHERE X
FROM
SELECT from1
FROM from2
WHERE from3
WHERE
SELECT child1
FROM child2
我想分三个部分:
SELECT select1
FROM
SELECT A
FROM B
WHERE C
WHERE X
和
SELECT from1
FROM from2
WHERE from3
和
SELECT child1
FROM child2
对于我拿出的每个部分,我都会做一个递归调用再次做同样的事情。 :)
答案 0 :(得分:1)
这个正则表达式适合你
(?=\t)((?:.|\n\t+)*)
<强> Regex Demo 强>
正则表达式细分
(?=\t) #This lookahead finds the position of \t
( #Capturing group
(?: #Non-capturing group
. #Match any character (but this does not matches \n.So we use alternation)
| #Alternation (OR)
\n\t+ #Match all lines \n followed by \t (as it seems from your input)
)* #Repeat this until any of the condition (mainly \n followed by \t) fails
)
注意: - 在python中使用它时,请确保它只是\t
而不是简单的空格。
Python代码
p = re.compile(r'(?=\t)((?:.|\n\t+)*)', re.MULTILINE)
test_str = "SELECT\n\tSELECT select1\n\tFROM\n\t\tSELECT A\n\t\tFROM B\n\t\tWHERE C\n\tWHERE X\nFROM\n\tSELECT from1\n\tFROM from2\n\tWHERE from3\nWHERE\n\tSELECT child1\n\tFROM child2"
print(re.findall(p, test_str))
<强> Ideone Demo 强>
答案 1 :(得分:0)
这非常简单:^\s(.*)
以下是在代码中使用它的方法:
import re
str = "Your text here"
p = re.compile(ur'^\s(.*), re.MULTILINE)
re.findall(p, str)
^
主播开始\s
匹配空格(.*)
将所有内容匹配到行尾