Python and Regular Expressions

Discussion in 'Python' started by Richard Lamboj, Apr 7, 2010.

  1. Hello,

    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}

    Kind Regards

    Richi
    Richard Lamboj, Apr 7, 2010
    #1
    1. Advertising

  2. Richard Lamboj a écrit :
    > Hello,
    >
    > 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
    > }
    > }


    (snip)

    I think you'd be better writing a specific parser here. Paul McGuire's
    PyParsing package might help:

    http://pyparsing.wikispaces.com/

    My 2 cents.
    Bruno Desthuilliers, Apr 7, 2010
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jay Douglas
    Replies:
    0
    Views:
    593
    Jay Douglas
    Aug 15, 2003
  2. P.Hill
    Replies:
    6
    Views:
    6,305
    P.Hill
    Nov 18, 2003
  3. Tony C
    Replies:
    6
    Views:
    319
  4. Patrick Maupin

    Re: Python and Regular Expressions

    Patrick Maupin, Apr 8, 2010, in forum: Python
    Replies:
    14
    Views:
    476
    Neil Cerutti
    Apr 12, 2010
  5. Noman Shapiro
    Replies:
    0
    Views:
    220
    Noman Shapiro
    Jul 17, 2013
Loading...

Share This Page