Hey all, in perl I was able to use the same regular expression multiple times changing one part of it
via a previously defined array and put the results into a new array
IN PERL:
my @targets = ('OVERALL RATING',
'CLOTHING', '
ITEMS',
'ACCESSORIES',
'SHOES',
'FINE JEWELRY');
my @JA13 = map {
$file2 =~/$_.*?(?:(\d{1,3}\.\d)\s+){3}/s;
} @targets;
So, in python instead of
match2 = re.search('OVEWRALL RATING.*?(?:(\d{1,3}\.\d)\s+){3} ', file2);m01 = match2.group(1) ;print m01
match2 = re.search('CLOTHING.*?(?:(\d{1,3}\.\d)\s+){3} ', file2); m02 = match2.group(1) ;print m02
match2 = re.search('ITEMS.*?(?:(\d{1,3}\.\d)\s+){3} ', file2); m03 = match2.group(1) ;print m03
match2 = re.search('ACCESSORIES.*?(?:(\d{1,3}\.\d)\s+){3} ', file2); m04 = match2.group(1) ;print m04
match2 = re.search('SHOES.*?(?:(\d{1,3}\.\d)\s+){3} ', file2); m05 = match2.group(1) ;print m05
match2 = re.search('FINE JEWELRY.*?(?:(\d{1,3}\.\d)\s+){3} ', file2); m06 = match2.group(1) ;print m06
I would have something similar to perl above:
targets = ['OVERALL RATING',
'CLOTHING', ITEMS',
'ACCESSORIES',
'SHOES',
'FINE JEWELRY']
PROPOSED CODE:
match2 = re.search(targets[i].*?(?:(\d{1,3}\.\d)\s+){3} ', file2);m[i] = match2.group(1)
Lance 5 1235
I don't like string interpolation within REs, it pops me out of 'RE
mode' as I scan the line.
Maybe creating a dict of matchobjects could be used in the larger
context?:
dict( [(t, re.search(t+regexp_tail, file2) for t in targets] )
(untested).
- Pad.
Think about how well the above solutions scale as len(targets)
increases.
1. Make "targets" a set, not a list.
2. Have *ONE* regex which includes a bracketed match for a generic
target e.g. ([A-Z\s]+)
3. Do *ONE* regex search, not 1 per target
4. If successful, check if the bracketed gizmoid is in the set of
targets.
It's not 100% apparent whether it is possible that there can be more
than one target in the inappropriately named file2 (it is a string,
isn't it?). If so, then write your own findall-like loop wrapped
around steps 2 & 3 above. Compile the regex in advance.
HTH,
John
Would you believe "steps 3 & 4"?
"Dennis Lee Bieber" <wl*****@ix.netcom.com> wrote in message
news:41********************************@4ax.com... On Mon, 15 May 2006 19:41:39 -0500, Lance Hoffmeyer <la***@augustmail.com> declaimed the following in comp.lang.python: I would have something similar to perl above:
targets = ['OVERALL RATING', 'CLOTHING', ITEMS', 'ACCESSORIES', 'SHOES', 'FINE JEWELRY']
PROPOSED CODE:
match2 = re.search(targets[i].*?(?:(\d{1,3}\.\d)\s+){3} ', file2);m[i] =
match2.group(1) I don't do regex's, and I also don't find packing multiple statements on
one line attractive.
I concur - this kind of multiple-statements-per-line-ishness feels
gratuitous. Yes, I know they line up nicely when all 6 statements are
printed out together, but the repetition of "mNN = match2.group(1) ;print
mNN" should tell you that this might be better done with a for loop. DRY. However... Why don't you basically do what you say you do in Python... Substitute you targets into the expression while inside a loop...
targets = [ "OVERALL RATING", "CLOTHING", "ITEMS", "ACCESSORIES", "SHOES", "FINE JEWELRY" ]
results = [] for t in target: m2 = re.search("%s.*?(?:(\d{1,3}\.\d)\s+){3}" % t, file2) results.append(m2.group(1)) --
# by contrast, here is a reasonably Pythonic one-liner, if one-liner it must
be
results = [ re.search(r"%s.*?(?:(\d{1,3}\.\d)\s+){3}" % t, file2).group(1)
for t in targets ]
# or for improved readability (sometimes 2 lines are better than 1):
reSearchFunc = lamdba tt,ff : re.search(tt + r".*?(?:(\d{1,3}\.\d)\s+){3}",
ff).group(1)
results = [ reSearchFunc(t,file2) for t in targets ]
Resisting-the-urge-to-plug-pyparsing-ly yours,
-- Paul
John Machin wrote: Would you believe "steps 3 & 4"?
How about "two pops and a pass?"
Quick! Lower the cone of silence!
--
Edward Elliott
UC Berkeley School of Law (Boalt Hall)
complangpython at eddeye dot net This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Medi Montaseri |
last post by:
Hope this is a right forum, else route me...
I'm using map STL in Microsoft Visual C++ 6.0 and am getting
warning C4786 which seems to have to do with truncating symbols
as VC++ is building a...
|
by: Christof Krueger |
last post by:
Hello,
I'm quite new to C++ so maybe there's something I miss.
I write a simple board game. It has a board class. This class has a
method that returns the count of pieces a player has on the...
|
by: Bryan Pietrzak |
last post by:
I'm new to using the STL and templates and while I've had no problem
at all using vector, I'm having a problem using map.
I just want to do something like:
class CMyData
{
// class stuff...
|
by: Weddick |
last post by:
I decided to try creating a map with microsoft visual C++ 6. When building
this small app I get 95 warnings which make no sense to me. Anybody else
see this before?
Thanks,
// CODE SAMPLE...
|
by: Gernot Frisch |
last post by:
Can I do this:
std::map<long, std::vector<long>> snapmap;
long i=4, j=0;
snapmap.push_back(i);
--
-Gernot
int main(int argc, char** argv) {printf
| |
by: Erik Arner |
last post by:
Hi,
let's say I have a std::map<std::string,int> and I want to search the map
for all keys that start with "foo". The regexp equivalent is to search for
"foo*", or perhaps "^foo*".
At present...
|
by: George Ter-Saakov |
last post by:
Hi.
I need some info where to look for following data object (aka map).
It acts as a map but accept a Regexp as a key. (support of * and ? will be
sufficient).
So if i try look for string it...
|
by: mirkmmd |
last post by:
Hi
I am new to this site and also to c++. The problem I am facing is that I when I run the following code, I expect to get '"Today is sunny" at the end of the program. And I do get it in Visual...
|
by: puzzlecracker |
last post by:
I need to be able to use map<char, T*>, I can make it
std::map<std::string, T*>, however, Key is passed by char, hence I
would need to call c_str() all the time. What do you suggest?
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| | |