469,934 Members | 1,743 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,934 developers. It's quick & easy.

multiple pattern regular expression


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-patte...p16895148.html
Sent from the Python - python-list mailing list archive at Nabble.com.

Jun 27 '08 #1
9 2284
micron_make <mi***********@yahoo.comwrites:
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
Jun 27 '08 #2
Arnaud Delobelle wrote:
micron_make <mi***********@yahoo.comwrites:

>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
Jun 27 '08 #3
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
Jun 27 '08 #4
On Fri, Apr 25, 2008 at 09:50:56AM -0400, py****@bdurham.com 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 (ni**@stinemates.org)
http://nick.stinemates.org
Jun 27 '08 #5
Nick Stinemates wrote:
On Fri, Apr 25, 2008 at 09:50:56AM -0400, py****@bdurham.com 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
Jun 27 '08 #6
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, py****@bdurham.com 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 (ni**@stinemates.org)
http://nick.stinemates.org
Jun 27 '08 #7
On Apr 25, 8:37*pm, Arnaud Delobelle <arno...@googlemail.comwrote:
micron_make <micro_pass...@yahoo.comwrites:
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
Jun 27 '08 #8
Chris Henry <ch***********@gmail.comwrites:
On Apr 25, 8:37*pm, Arnaud Delobelle <arno...@googlemail.comwrote:
>micron_make <micro_pass...@yahoo.comwrites:
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
Jun 27 '08 #9

each of the suggested methods fit in. I have started trying them.

-Rohit
--
View this message in context: http://www.nabble.com/multiple-patte...p16936657.html
Sent from the Python - python-list mailing list archive at Nabble.com.

Jun 27 '08 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Johann Sijpkes | last post: by
5 posts views Thread by Abby Lee | last post: by
32 posts views Thread by tshad | last post: by
2 posts views Thread by Alphonse Giambrone | last post: by
3 posts views Thread by gast128 | last post: by
5 posts views Thread by mikko.n | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.