multiple pattern regular expression

M

micron_make

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
 
A

Arnaud Delobelle

micron_make said:
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.
 
R

Robert Bossy

Arnaud said:
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
 
P

python

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
 
N

Nick Stinemates

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

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
 
C

Chris Henry

micron_make said:
I am trying to parse a file whose contents are :
[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
 
A

Arnaud Delobelle

Chris Henry said:
micron_make said:
I am trying to parse a file whose contents are :
[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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top