简体   繁体   中英

Parse through text files, and grab words in between unique words, using python

I am trying to build a parser that will go through some SQL scripts and grab the data fields, tables etc. being used. I started it by trying to grab the field names. The SQL scripts all have a basic structure of:

select x,y,z,.. from table # sometimes it will be sel instead of select

This occurs usually multiple times in any script.

I have setup the below python code:

import csv
import re

def parser():                                     

           f=open('Book1.txt','r')
           data = f.read()
           print re.findall('sel.*from',data) 

I am only getting one of the select statements through this. Why is this not giving me all the texts between my select statements from which I can then parse through and determine the data fields being used? Maybe there is a better way to do this but I am hitting a wall.

Your regular expression is probably matching multiple select statements because it's working in greedy mode. Try using re.findall('sel.*?from', data) instead.

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