[BangPypers] Question on Pattern Matching and Regular Expression

davidsnt davidsnt at gmail.com
Mon Jan 7 11:22:48 CET 2013


Gora,

Can you help me with few links that you have handy to which I can refer to
build a parser instead of RE

Regards,
Davidsanthosh L


On Mon, Jan 7, 2013 at 3:38 PM, Gora Mohanty <gora at mimirtech.com> wrote:

> On 7 January 2013 15:06, davidsnt <davidsnt at gmail.com> wrote:
> > Bangpypers,
> >
> > Having a little trouble in parsing a file of 702 line appox,
> >
> > the file is in the format
> >
> > #
> > # <Title>
> > #
> > [Space]
> > [
> >
> > Few lines of information about the title
> >
> > ]
> >
> > [Space]
>
> If the above format is strictly followed, this should do it,
> assuming you can read the entire file into a string (s in
> the example below.
>
> import re
> TITLE_RE = re.compile( r'#\n#([^\n]*)\n#\n \n\[([^\]]*)\]\n \n',
> re.MULTILINE|re.DOTALL )
> for m in TITLE_RE.finditer( s.strip ):
>      title, info = m.groups()
>      print title, info
>
> Error handling, and reading chunks from a large file
> are left as an exercise for the reader.
>
> Also, if the file format is at all more complex, and
> maybe even in this case, I would write a parser
> rather than use regular expressions.
>
> Regards,
> Gora
> _______________________________________________
> BangPypers mailing list
> BangPypers at python.org
> http://mail.python.org/mailman/listinfo/bangpypers
>


More information about the BangPypers mailing list