Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: Help with regular expression in python

Reply
Thread Tools

Re: Help with regular expression in python

 
 
Vlastimil Brom
Guest
Posts: n/a
 
      08-22-2011
Sorry, if I missed some further specification in the earlier thread or
if the following is oversimplification of the original problem (using
3 numbers instead of 32),
would something like the following work for your data?

>>> import re
>>> data = """2.201000e+01 2.150000e+01 2.199000e+01 : (instance: 0) : some description

.... 2.201000e+01 2.150000e+01 2.199000e+01 : (instance: 0) :
some description
.... 2.201000e+01 2.150000e+01 2.199000e+01 : (instance: 0) :
some description
.... 2.201000e+01 2.150000e+01 2.199000e+01 : (instance: 0) :
some description"""
>>> for res in re.findall(r"(?m)^(??:[-+]?(?:\d+(?:\.\d*)?|\.\d+)(?:[eE][-+]?\d+))?\s+){3}(?:.+)$", data): print res

....
2.201000e+01 2.150000e+01 2.199000e+01 : (instance: 0) :
some description
2.201000e+01 2.150000e+01 2.199000e+01 : (instance: 0) :
some description
2.201000e+01 2.150000e+01 2.199000e+01 : (instance: 0) :
some description
2.201000e+01 2.150000e+01 2.199000e+01 : (instance: 0) :
some description
>>>


i.e. all parentheses are non-capturing (?:...) and there are extra
anchors for line begining and end ^...$ with the multiline flag set
via (?m)
Each result is one matching line in this sample (if you need to acces
single numbers, you could process these matches further or use the new
regex implementation mentioned earlier by mrab (its developer) with
the new match method captures() - using an appropriate pattern with
the needed groupings).

regards,
vbr
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Seek xpath expression where an attribute name is a regular expression GIMME XML 3 12-29-2008 03:11 PM
C/C++ language proposal: Change the 'case expression' from "integral constant-expression" to "integral expression" Adem C++ 42 11-04-2008 12:39 PM
C/C++ language proposal: Change the 'case expression' from "integral constant-expression" to "integral expression" Adem C Programming 45 11-04-2008 12:39 PM
Matching abitrary expression in a regular expression =?iso-8859-1?B?bW9vcJk=?= Java 8 12-02-2005 12:51 AM
Dynamically changing the regular expression of Regular Expression validator VSK ASP .Net 2 08-24-2003 02:47 PM



Advertisments