Regular Expression question

Marc 'BlackJack' Rintsch bj_666 at gmx.net
Thu Oct 25 02:49:06 EDT 2007


On Thu, 25 Oct 2007 06:34:03 +0000, looping wrote:

> Hi,
> It's not really a Python question but I'm sure someone could help me.
> 
> When I use RE, I always have trouble with this kind of search:
> 
> Ex.
> 
> I've a text file:
> """
> create or replace package XXX
> ...
> 
> create or replace package body XXX
> ...
> """
> now I want to search the position (line) of this two string.
> 
> for the body I use:
> s = re.search(r'create\s+or\s+replace\s+package\s+body\s+', txt,
> re.IGNORECASE)
> 
> but how to search for the other line ?
> I want the same RE but explicitly without "body".

The write the same RE but explicitly without "body".  But I guess I didn't
understand your problem when the answer is that obvious.

Maybe you want to iterate over the text file line by line and match or
search within the line? Untested:

needle = re.compile(r'create\s+or\s+replace\s+package(\s+body)?\s+',
                    re.IGNORECASE)
for i, line in enumerate(lines):
    if needle.match(line):
        print 'match in line %d' % (i + 1)

Ciao,
	Marc 'BlackJack' Rintsch



More information about the Python-list mailing list