Regular Expression - newbie question

Raymond Hettinger python at rcn.com
Wed Aug 21 02:48:37 EDT 2002


Try something like this:

re.findall("^.*python output[\000-\377]*^.*?Hello World[\000-\377].*$",
data)

Note the question mark added after the star to make the middle part
non-greedy.


Raymond Hettinger





"SK" <skunix at hotmail.com> wrote in message
news:ad5b37b7.0208201846.77c2251b at posting.google.com...
> I want to parse a file for the following:-
>
> Line containing "python output" followed by anything and then "Hello
> World"
> The following code snippet works fine but How do I know that two such
> patterns exist in the file.
>
> Is it possible to mask the *anything* to be output on match.group()
> i.e. The output should NOT have "This is my file"
>
> I want something like this:-
>
> Desired Output:-
> ==============
>
> Pattern 1
>
> This is one python output
> Hello World
>
> Pattern 2
>
> This is two python output
> Hello World
>
> Total matches found is 2.
>
> PS: Also, my regular expression is determined dynamically by the user.
>
>
> Code Snippet
> ============
>
> import re
> data = open("c.txt","rb").read()
> regexp = "^.*python output[\000-\377]*^.*Hello World[\000-\377].*$"
> r = re.compile(regexp,re.M)
> match = re.search(r, data)
> print match.group()
>
> c.txt
> =====
>
> This is my file
> This is one python output
> This is my file
> This is my file
> Hello World
> This is my file
> This is two python output
> This is my file
> Hello World
>
>
> Output
> ======
> This is one python output
> This is my file
> This is my file
> Hello World
> This is my file
> This is two python output
> This is my file
> Hello World
>
> Thanks in Advance





More information about the Python-list mailing list