Extract all words between two keywords in .txt file (Python)

A S aishan0403 at gmail.com
Wed Dec 11 12:00:19 EST 2019


I would like to extract all words within specific keywords in a .txt file. For the keywords, there is a starting keyword of "PROC SQL;" (I need this to be case insensitive) and the ending keyword could be either "RUN;", "quit;" or "QUIT;". This is my sample .txt file.

Thus far, this is my code:

with open('lan sample text file1.txt') as file:
    text = file.read()
    regex = re.compile(r'(PROC SQL;|proc sql;(.*?)RUN;|quit;|QUIT;)')
    k = regex.findall(text)
    print(k)


Output:

[('quit;', ''), ('quit;', ''), ('PROC SQL;', '')]

However, my intended output is to get the words in between and inclusive of the keywords:

proc sql; ("TRUuuuth");
hhhjhfjs as fdsjfsj:
select * from djfkjd to jfkjs
(
SELECT abc AS abc1, abc_2_ AS efg, abc_fg, fkdkfj_vv, jjsflkl_ff, fjkdsf_jfkj
    FROM &xxx..xxx_xxx_xxE
where ((xxx(xx_ix as format 'xxxx-xx') gff &jfjfsj_jfjfj.) and 
      (xxx(xx_ix as format 'xxxx-xx') lec &jgjsd_vnv.))
 );

1)

jjjjjj;

  select xx("xE'", PUT(xx.xxxx.),"'") jdfjhf:jhfjj from xxxx_x_xx_L ;
quit; 

PROC SQL; ("CUuuiiiiuth");
hhhjhfjs as fdsjfsj:
select * from djfkjd to jfkjs
(SELECT abc AS abc1, abc_2_ AS efg, abc_fg, fkdkfj_vv, jjsflkl_ff, fjkdsf_jfkj
    FROM &xxx..xxx_xxx_xxE
where ((xxx(xx_ix as format 'xxxx-xx') gff &jfjfsj_jfjfj.) and 
      (xxx(xx_ix as format 'xxxx-xx') lec &jgjsd_vnv.))(( ))
 );

2)(

RUN;


__________________________________________________________
Any advice or different ways to go about this would be greatly appreciated!


More information about the Python-list mailing list