473,382 Members | 1,407 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

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 2466
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Johann Sijpkes | last post by:
Hi, Just before going mad I thought asking this newsgroup would be a good idea. I want to validate dates using a schema but somehow the regexp is not performed by the validator? (at w3c..). I...
1
by: Jeff | last post by:
For the life of me, I can't create a working regular expression for a schema pattern to do the following: Validates if an entire word does NOT match the entire word in the pattern. For...
5
by: Abby Lee | last post by:
I ask the user to enter a time in the formatio 12:30 PM. onChange I send the string to this function. I'm using alert boxes to test it...and am always getting the "Does not work" alert box. What am...
4
by: aevans1108 | last post by:
expanding this message to microsoft.public.dotnet.xml Greetings Please direct me to the right group if this is an inappropriate place to post this question. Thanks. I want to format a...
32
by: tshad | last post by:
Can you do a search for more that one string in another string? Something like: someString.IndexOf("something1","something2","something3",0) or would you have to do something like: if...
2
by: Alphonse Giambrone | last post by:
Is there a way to use multiple search patterns when calling Directory.GetFiles. For instance Directory.GetFiles("C:\MyFolder", "*.aspx") will return all files with the aspx extension. But what if...
3
by: gast128 | last post by:
Dear all, I was looking for a c++ implementation of regular expressions using the Interpreter Design Pattern. GOF only references a Smaltalk implementation which I can not read :(. Does...
10
by: ll | last post by:
Hi, I currently am using the following regex in js for email validation, in which the email addresses can be separated by commas or semicolons. The problem, however, lies in that I can type two...
5
by: mikko.n | last post by:
I have recently been experimenting with GNU C library regular expression functions and noticed a problem with pattern matching. It seems to recognize only the first match but ignoring the rest of...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.