Shortcut needed: avoiding temporary variables with regular expressions

Discussion in 'Python' started by Pekka Niiranen, Nov 6, 2004.

  1. 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-
    Pekka Niiranen, Nov 6, 2004
    #1
    1. Advertising

  2. Pekka Niiranen <> 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
    Alex Martelli, Nov 6, 2004
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jay Douglas
    Replies:
    0
    Views:
    593
    Jay Douglas
    Aug 15, 2003
  2. Andreas Klemt

    Regular Expressions help needed

    Andreas Klemt, Aug 17, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    301
    Andreas Klemt
    Aug 17, 2004
  3. Andreas Klemt

    Regular Expressions Guru needed. Please help!

    Andreas Klemt, Aug 18, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    280
    Andreas Klemt
    Aug 18, 2004
  4. Replies:
    7
    Views:
    3,181
    James Kanze
    Feb 12, 2008
  5. Noman Shapiro
    Replies:
    0
    Views:
    221
    Noman Shapiro
    Jul 17, 2013
Loading...

Share This Page