简体   繁体   中英

Python regex extract lines between substrings that start with certain criteria

I've edited this to be more clear and provide a solution.

I'd like to use regex to search within substrings for text starting with the characters -- and extract all text that comes after for that line. I already have both pieces successfully working separately (1. the substring search and 2. the -- search), I'm just not sure how to combine these effectively. I have a string -

qry = ''' 
with 
qry_1 as ( -- some text
   SELECT ID, 
          NAME
   FROM   ( ... other code...
),
qry_2 as ( 
    SELECT coalesce (table1.ID, table2.ID) as ID,
           NAME
   FROM (...other code...
),
qry_3 as (
-- some text
     SELECT id.WEATHER AS WEATHER_MORN,
            ROW_NUMBER() OVER(PARTITION BY id.SUN
                ORDER BY id.TIME) AS SUN_TIME,
            id.RAIN,
            id.MIST
   FROM (...other code..
-- some other text
)
'''
  1. I'm able to extract subquery information through re.findall here -
sub = re.findall('\),\s{2,}(.*?)as\s\(',qry)
  1. And the special character search
re.findall(r'--+(.*)(.\s)',qry)
  1. To ultimately use to search between strings for the commentary.

But how to incorporate step 3 successfully?

Thank you for guidance here

Step 3 successfully run

commentary = [re.findall(r'--+(.*)(.\s)', i) 
     for i in re.findall('\),\s{2,}(.*?)as\s\(',qry,flags=re.S)]

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