[python] Problem s regularnym vyrazom

Jan Janech devel na atlas.sk
Čtvrtek Únor 1 16:51:00 CET 2007


Na taketo veci sa hodia skor parser-generatory ako regularne vyrazy. Ja 
pouzivam YAPPS (http://theory.stanford.edu/~amitp/yapps/). Konkretne SQL 
som s nim uz parsoval.

RMiklos na pss.sk wrote:
> Rad by som tymto oslovil odbornikov na regularne vyrazy.
> 
> Extrahujem datove polia tabulky cez regularny vyraz takto:
> --------------------------- skript ---------------------------
> import sys,re
> 
> sql_statement='''CREATE TABLE IBPDDB071/R004660V
> (PERSONNR    DECIMAL     (10, 0)    NOT NULL WITH DEFAULT, LFDNR1 DECIMAL  
>   (3, 0)     NOT NULL WITH DEFAULT, VOTYP       CHARACTER   (1)        NOT 
> NULL WITH DEFAULT, BSSTAMMNR   DECIMAL     (7, 0)     NOT NULL WITH 
> DEFAULT,BSVNR       DECIMAL     (2, 0)     NOT NULL WITH DEFAULT );'''
> regexp_sql_create=\
> re.compile(r"^\s*CREATE\s+TABLE\s+"
>            r"(?P<library>[A-Z0-9]+)\s*[/.]\s*(?P<file>[A-Z0-9]+)\s*"
>            r"\((?P<tbl_field_def>.*)\)\s*")
> 
> result=regexp_sql_create.search(sql_statement)
> if result == None:
>   # CREATE TABLE statement not valid ! 
>   print "SQL CREATE TABLE statement is not valid !!!"
>   sys.exit()
> else:
>   # Parse data from RegExp
>   library=result.group('library')
>   print "Library=%s" % library
>   table=result.group('file')
>   print "Table=%s" %table
>   table_field_def=result.group('tbl_field_def')
>   print "Field Definitions=%s" % table_field_def
> ------------------------ konec skriptu ------------------------
> 
> Potom definicie dat. poli dalej pracovavam. 
> Toto by fungovalo, az na to ze SQL-skript je vygenerovany a okrem CREATE 
> TABLE (...); moze obsahovat aj LABEL ON (...);
> takze moze vyzerat takto:
> 
> sql_statement='''CREATE TABLE IBPDDB071/R004660V
> (PERSONNR    DECIMAL     (10, 0)    NOT NULL WITH DEFAULT, LFDNR1 DECIMAL  
>   (3, 0)     NOT NULL WITH DEFAULT, VOTYP       CHARACTER   (1)        NOT 
> NULL WITH DEFAULT, BSSTAMMNR   DECIMAL     (7, 0)     NOT NULL WITH 
> DEFAULT,BSVNR       DECIMAL     (2, 0)     NOT NULL WITH DEFAULT );  Label 
> on IBPDDB071/R004660V(PERSONNR   IS 'PERSONNR', LFDNR1     IS 'LFDNR1', 
> VOTYP      IS 'VOTYP', BSSTAMMNR  IS 'BSSTAMMNR', BSVNR      IS 
> 'BSVNR');'''
> 
> Vtedy mi horeuvedeny regexp matchuje (pretoze je greedy) vsetko, takze 
> rozpozna vsetko od 'CREATE TABLE(' az po poslednu zatvorku ')' stringu 
> t.j. '..); LABEL ON(..' ale ja potrebujem aby rozpoznal len to co je v 
> tele CREATE TABLE. 
> 
> Vie mi niekto poradit ako napisat ten regularny vyraz aby robil co 
> potrebujem?
> Mgr. Ing. Roman MIKLÓ© 
> Prvá stavebná sporiteµňa a.s. 
> Bajkalská 30, P. O. Box 48 
> 829 48  Bratislava 25 
> Tel.: +421/ 2 / 582 31 174 
> Fax: +421/ 2 / 582 31 109 


Daląí informace o konferenci Python