473,320 Members | 1,936 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,320 software developers and data experts.

Shortcut needed: avoiding temporary variables with regular expressions

Hi there,

I am using regular expressions like this:

matcher = re.compile(r'(.*)\s(.*)', re.UNICODE)
tmp = matcher.search(mystring)
if tmp:
myvariable = tmp.group(1)

The idea is that group(1) is accessed only if
it was found from 'mystring'. Is there a way
to avoid the usage of 'tmp' -variable?
Sure, I could try:

matcher = re.compile(r'(.*)\s(.*)', re.UNICODE)
if matcher.search(mystring):
myvariable = matcher.search(mystring).group(1)

but then I am searching 'mystring' twice.

-pekka-
Jul 18 '05 #1
1 1230
Pekka Niiranen <pe************@wlanmail.com> wrote:
Hi there,

I am using regular expressions like this:

matcher = re.compile(r'(.*)\s(.*)', re.UNICODE)
tmp = matcher.search(mystring)
if tmp:
myvariable = tmp.group(1)

The idea is that group(1) is accessed only if
it was found from 'mystring'. Is there a way
to avoid the usage of 'tmp' -variable?


Yeah, quite a few recipes on the cookbook could help, starting with one
of mine, many years old now, about how to do assign-and-test together in
Python (the only use case being to transcribe faithfully an algorithm
coming from languages that focus on assign-and-test, such as Perl or C;
once you move the algorithm fully to Python, it's best to use Python
idioms, of course).

In my old recipe,, you need to have somewhere an auxiliary class and
instance thereof, such as:

class Data(object):
def set(self, value):
self.value = value
return value
d = Data()

then, you can do:

if d.set(matcher.search(mystring)):
myvariable = d.value.group(1)

This is very general and generic. For your specific case, you could do:

try: myvariable = matcher.search(mystring).group(1)
except (AttributeError, TypeError): pass

i.e., just try to access .group even if the search results in None, and
simply catch the error that happens when you do (I think it will be an
AttributeError, but I'm not sure that holds in all versions you care
about, so I threw in a TypeError for good measure;-).
Alex
Jul 18 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Patryk Konieczka | last post by:
Hello Here's the thing I have a database edited by some company workers editing descriptions of books in the sotre , unfortunately these workers do not have the habit of inserting a space...
5
by: Karin Jensen | last post by:
Hi I am writing in PHP and trying to work with regular expressions on records in a multilanguage database. I understand regexp basics, but have bitten off more than I can chew here and really...
27
by: VK | last post by:
<http://www.jibbering.com/faq/#FAQ3_2> The parts where update, replacement or add-on is needed are in <update> tag. 3.2 What online resources are available? Javascript FAQ sites, please...
4
by: Neri | last post by:
Some document processing program I write has to deal with documents that have headers and footers that are unnecessary for the main processing part. Therefore, I'm using a regular expression to go...
2
by: ajitgoel | last post by:
Hi; I need some simple help with my regular expressions. I want to search my input text for all the boolean variables which do not start with bln. i.e I want to match "bool followed by 1 or...
42
by: Dooglo | last post by:
I'm new VB and programming all together, but I'm getting he hang of it. My question is; I'm writting a program to figure square feet and yards when the user inputs "Length in feet and inch (...
4
by: Együd Csaba | last post by:
Hi All, I'd like to "compress" the following two filter expressions into one - assuming that it makes sense regarding query execution performance. .... where (adate LIKE "2004.01.10 __:30" or...
3
by: Ben | last post by:
Hi, I am trying to create a specific structure, but having problems with the type definitions. Basically it's an array of trees. Each element of the array should be a binary tree of type...
12
by: Kira Yamato | last post by:
I've posted this in another thread, but I suppose I should've started a new thread for it instead. I cannot get the following short program to compile under g++: #include <iostream> #include...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
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...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
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...

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.