Python and Regular Expressions

Richard Lamboj richard.lamboj at bilcom.at
Wed Apr 7 05:44:30 EDT 2010


Am Wednesday 07 April 2010 10:52:14 schrieb Chris Rebert:
> On Wed, Apr 7, 2010 at 1:37 AM, Richard Lamboj <richard.lamboj at bilcom.at> 
wrote:
> > i want to parse this String:
> >
> > version 3.5.1 {
> >
> >        $pid_dir = /opt/samba-3.5.1/var/locks/
> >        $bin_dir = /opt/samba-3.5.1/bin/
> >
> >        service smbd {
> >                bin = ${bin_dir}smbd -D
> >                pid = ${pid_dir}smbd.pid
> >        }
> >        service nmbd {
> >                bin = ${bin_dir}nmbd -D
> >                pid = ${pid_dir}nmbd.pid
> >        }
> >        service winbindd {
> >                bin = ${bin_dir}winbindd -D
> >                pid = ${pid_dir}winbindd.pid
> >        }
> > }
> >
> > version 3.2.14 {
> >
> >        $pid_dir = /opt/samba-3.5.1/var/locks/
> >        $bin_dir = /opt/samba-3.5.1/bin/
> >
> >        service smbd {
> >                bin = ${bin_dir}smbd -D
> >                pid = ${pid_dir}smbd.pid
> >        }
> >        service nmbd {
> >                bin = ${bin_dir}nmbd -D
> >                pid = ${pid_dir}nmbd.pid
> >        }
> >        service winbindd {
> >                bin = ${bin_dir}winbindd -D
> >                pid = ${pid_dir}winbindd.pid
> >        }
> > }
> >
> > Step 1:
> >
> > version 3.2.14 {
> >
> >        $pid_dir = /opt/samba-3.5.1/var/locks/
> >        $bin_dir = /opt/samba-3.5.1/bin/
> >
> >        service smbd {
> >                bin = ${bin_dir}smbd -D
> >                pid = ${pid_dir}smbd.pid
> >        }
> >        service nmbd {
> >                bin = ${bin_dir}nmbd -D
> >                pid = ${pid_dir}nmbd.pid
> >        }
> >        service winbindd {
> >                bin = ${bin_dir}winbindd -D
> >                pid = ${pid_dir}winbindd.pid
> >        }
> > }
> >
> > Step 2:
> >        service smbd {
> >                bin = ${bin_dir}smbd -D
> >                pid = ${pid_dir}smbd.pid
> >        }
> > Step 3:
> >        $pid_dir = /opt/samba-3.5.1/var/locks/
> >        $bin_dir = /opt/samba-3.5.1/bin/
> >
> > Step 4:
> >                bin = ${bin_dir}smbd -D
> >                pid = ${pid_dir}smbd.pid
> >
> > My Regular Expressions:
> > version[\s]*[\w\.]*[\s]*\{[\w\s\n\t\{\}=\$\.\-_\/]*\}
> > service[\s]*[\w]*[\s]*\{([\n\s\w\=]*(\$\{[\w_]*\})*[\w\s\-=\.]*)*\}
> >
> > I think it was no good Solution. I'am trying with Groups:
> > (service[\s\w]*)\{([\n\w\s=\$\-_\.]*)
> > but this part makes Problems: ${bin_dir}
>
> Regular expressions != Parsers
>
> Every time someone tries to parse nested structures using regular
> expressions, Jamie Zawinski kills a puppy.
>
> Try using an *actual* parser, such as Pyparsing:
> http://pyparsing.wikispaces.com/
>
> Cheers,
> Chris
> --
> Some people, when confronted with a problem, think:
> "I know, I'll use regular expressions." Now they have two problems.
> http://blog.rebertia.com

Well, after some trying with regex, your both right. I will use pyparse it 
seems to be the better solution.

Kind Regards


More information about the Python-list mailing list