简体   繁体   中英

Regular expression pattern (match newline and tab) in Python

I am using Python. Please help me to find Regex pattern for this:

SELECT
    SELECT select1
    FROM
        SELECT A
        FROM B
        WHERE C
    WHERE X
FROM
    SELECT from1
    FROM from2
    WHERE from3
WHERE
    SELECT child1
    FROM child2

I want to take out three parts:

SELECT select1
FROM
    SELECT A
    FROM B
    WHERE C
WHERE X

and

    SELECT from1
    FROM from2
    WHERE from3

and

    SELECT child1
    FROM child2

With each part that I took out, I'll do a recursive call to do the same stuff again. :)

This regex will work for you

(?=\t)((?:.|\n\t+)*)

Regex Demo

Regex Breakdown

(?=\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
  )

NOTE :- While using this in python make sure that it is \\t only and not simple space.

Python Code

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

It's super simple: ^\\s(.*)

Here's how to use it in code:

import re

str = "Your text here"
p = re.compile(ur'^\s(.*), re.MULTILINE)
re.findall(p, str)

Explanation

  • ^ Anchor to line start
  • \\s Match a space
  • (.*) Match everything until end of line

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM