multiple pattern regular expression

Discussion in 'Python' started by micron_make, Apr 25, 2008.

  1. micron_make

    micron_make Guest

    I am trying to parse a file whose contents are :

    parameter=current
    max=5A
    min=2A

    for a single line I used
    for line in file:
    print re.search("parameter\s*=\s*(.*)",line).groups()

    is there a way to match multiple patterns using regex and return a
    dictionary. What I am looking for is (pseudo code)

    for line in file:
    re.search("pattern1" OR "pattern2" OR ..,line)

    and the result should be {pattern1:match, pattern2:match...}

    Also should I be using regex at all here ?

    -rohit


    --
    View this message in context: http://www.nabble.com/multiple-pattern-regular-expression-tp16895148p16895148.html
    Sent from the Python - python-list mailing list archive at Nabble.com.
     
    micron_make, Apr 25, 2008
    #1
    1. Advertising

  2. micron_make <> writes:

    > I am trying to parse a file whose contents are :
    >
    > parameter=current
    > max=5A
    > min=2A
    >
    > for a single line I used
    > for line in file:
    > print re.search("parameter\s*=\s*(.*)",line).groups()
    >
    > is there a way to match multiple patterns using regex and return a
    > dictionary. What I am looking for is (pseudo code)
    >
    > for line in file:
    > re.search("pattern1" OR "pattern2" OR ..,line)
    >
    > and the result should be {pattern1:match, pattern2:match...}
    >
    > Also should I be using regex at all here ?


    If every line of the file is of the form name=value, then regexps are
    indeed not needed. You could do something like that.

    params = {}
    for line in file:
    name, value = line.strip().split('=', 2)
    params[name] = value

    (untested)
    Then params should be the dictionary you want.

    --
    Arnaud
     
    Arnaud Delobelle, Apr 25, 2008
    #2
    1. Advertising

  3. micron_make

    Robert Bossy Guest

    Arnaud Delobelle wrote:
    > micron_make <> writes:
    >
    >
    >> I am trying to parse a file whose contents are :
    >>
    >> parameter=current
    >> max=5A
    >> min=2A
    >>
    >> for a single line I used
    >> for line in file:
    >> print re.search("parameter\s*=\s*(.*)",line).groups()
    >>
    >> is there a way to match multiple patterns using regex and return a
    >> dictionary. What I am looking for is (pseudo code)
    >>
    >> for line in file:
    >> re.search("pattern1" OR "pattern2" OR ..,line)
    >>
    >> and the result should be {pattern1:match, pattern2:match...}
    >>
    >> Also should I be using regex at all here ?
    >>

    >
    > If every line of the file is of the form name=value, then regexps are
    > indeed not needed. You could do something like that.
    >
    > params = {}
    > for line in file:
    > name, value = line.strip().split('=', 2)
    > params[name] = value
    >
    > (untested)

    I might add before you stumble upon the consequences:
    params[name.rstrip()] = value.lstrip()

    Cheers,
    RB
     
    Robert Bossy, Apr 25, 2008
    #3
  4. micron_make

    Guest

    How about this?

    for line in file:
    # ignore lines without = assignment
    if '=' in line:
    property, value = line.strip().split( '=', 1 )
    property = property.strip().lower()
    value = value.strip()

    # do something with property, value

    Malcolm
     
    , Apr 25, 2008
    #4
  5. On Fri, Apr 25, 2008 at 09:50:56AM -0400, wrote:
    > How about this?
    >
    > for line in file:
    > # ignore lines without = assignment
    > if '=' in line:
    > property, value = line.strip().split( '=', 1 )
    > property = property.strip().lower()
    > value = value.strip()
    >
    > # do something with property, value
    >
    > Malcolm
    > --
    > http://mail.python.org/mailman/listinfo/python-list


    This works until you have:
    string=The sum of 2+2=4

    For cases where such input my be expected, I use the following regex

    import re
    str = """a=b
    c=d
    e=f
    string=The sum of 2+2=4""".split("\n")

    p = re.compile("([^=]*)=(.*)")

    for lines in str:
    key,value=p.findall(lines)[0]
    print key, value


    --
    Nick Stinemates ()
    http://nick.stinemates.org
     
    Nick Stinemates, Apr 25, 2008
    #5
  6. Nick Stinemates wrote:
    > On Fri, Apr 25, 2008 at 09:50:56AM -0400, wrote:
    >> How about this?
    >>
    >> for line in file:
    >> # ignore lines without = assignment
    >> if '=' in line:
    >> property, value = line.strip().split( '=', 1 )
    >> property = property.strip().lower()
    >> value = value.strip()
    >>
    >> # do something with property, value
    >>
    >> Malcolm

    >
    > This works until you have:
    > string=The sum of 2+2=4


    Actually, it still works, because Malcolm used split()'s maxsplit parameter.

    --
    Carsten Haese
    http://informixdb.sourceforge.net
     
    Carsten Haese, Apr 26, 2008
    #6
  7. On Fri, Apr 25, 2008 at 08:40:55PM -0400, Carsten Haese wrote:
    > Nick Stinemates wrote:
    >> On Fri, Apr 25, 2008 at 09:50:56AM -0400, wrote:
    >>> How about this?
    >>>
    >>> for line in file:
    >>> # ignore lines without = assignment
    >>> if '=' in line:
    >>> property, value = line.strip().split( '=', 1 )
    >>> property = property.strip().lower()
    >>> value = value.strip()
    >>>
    >>> # do something with property, value
    >>>
    >>> Malcolm

    >> This works until you have:
    >> string=The sum of 2+2=4

    >
    > Actually, it still works, because Malcolm used split()'s maxsplit
    > parameter.
    >
    > --
    > Carsten Haese
    > http://informixdb.sourceforge.net
    > --
    > http://mail.python.org/mailman/listinfo/python-list


    Didn't see that

    --
    Nick Stinemates ()
    http://nick.stinemates.org
     
    Nick Stinemates, Apr 26, 2008
    #7
  8. micron_make

    Chris Henry Guest

    On Apr 25, 8:37 pm, Arnaud Delobelle <> wrote:
    > micron_make <> writes:
    > > I am trying to parse a file whose contents are :

    >
    > > parameter=current
    > > max=5A
    > > min=2A

    [snip]
    > If every line of the file is of the form name=value, then regexps are
    > indeed not needed.  You could do something like that.
    >
    > params = {}
    > for line in file:
    >     name, value = line.strip().split('=', 2)
    >     params[name] = value
    >
    > (untested)
    > Then params should be the dictionary you want.
    >

    I'm also interested in this problem. While this solution works, I'm
    looking for solution that will also check whether the parameter name/
    value is of a certain pattern (these patterns may be different, e.g.
    paramA, paramB, paramC may take integers value, while paramD may take
    true/false). Is there a way to do this easily?

    I'm new to Python and the solution I can think off involve a loop over
    a switch (a dictionary with name->function mapping). Is there other,
    more elegant solution?

    Chris
     
    Chris Henry, Apr 26, 2008
    #8
  9. Chris Henry <> writes:

    > On Apr 25, 8:37 pm, Arnaud Delobelle <> wrote:
    >> micron_make <> writes:
    >> > I am trying to parse a file whose contents are :

    >>
    >> > parameter=current
    >> > max=5A
    >> > min=2A

    > [snip]
    >> If every line of the file is of the form name=value, then regexps are
    >> indeed not needed.  You could do something like that.
    >>
    >> params = {}
    >> for line in file:
    >>     name, value = line.strip().split('=', 2)

    ^ 1, obviously!
    >>     params[name] = value
    >>
    >> (untested)
    >> Then params should be the dictionary you want.
    >>

    > I'm also interested in this problem. While this solution works, I'm
    > looking for solution that will also check whether the parameter name/
    > value is of a certain pattern (these patterns may be different, e.g.
    > paramA, paramB, paramC may take integers value, while paramD may take
    > true/false). Is there a way to do this easily?
    >
    > I'm new to Python and the solution I can think off involve a loop over
    > a switch (a dictionary with name->function mapping). Is there other,
    > more elegant solution?


    Sounds good to me.

    E.g.

    def boolean(x):
    if x == 'true':
    return True
    elif x == 'false'
    return False
    else:
    raise ValueError("Invalid boolean: '%s'" % x)

    paramtypes = { 'paramA': int, ..., 'paramD': boolean }

    #Then

    for line in file:
    line = line.strip()
    if not line: continue
    name, value = line.split('=', 1)
    if name in paramtypes:
    try:
    value = paramtypes[name](value)
    except ValueError, e:
    # handle the error
    value = e
    else:
    # What to do for untyped parameters
    pass
    params[name] = value

    --
    Arnaud
     
    Arnaud Delobelle, Apr 26, 2008
    #9
  10. micron_make

    micron_make Guest

    micron_make, Apr 28, 2008
    #10
    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. VSK
    Replies:
    2
    Views:
    2,388
  2. MARTIN Herve
    Replies:
    1
    Views:
    515
    Roedy Green
    Jul 22, 2003
  3. Johann Sijpkes
    Replies:
    2
    Views:
    474
    Johann Sijpkes
    Jul 14, 2004
  4. Stephen Kennedy

    Shell pattern to regular expression code

    Stephen Kennedy, Aug 6, 2003, in forum: Python
    Replies:
    0
    Views:
    301
    Stephen Kennedy
    Aug 6, 2003
  5. Vibha Tripathi

    Regular Expression for pattern substitution

    Vibha Tripathi, Jul 1, 2005, in forum: Python
    Replies:
    3
    Views:
    325
    Devan L
    Jul 1, 2005
Loading...

Share This Page