Shortcut needed: avoiding temporary variables with regular expressions

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Pekka Niiranen

    #1

    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-
  • Alex Martelli

    #2
    Re: Shortcut needed: avoiding temporary variables with regular expressions

    Pekka Niiranen <pekka.niiranen @wlanmail.com> wrote:
    [color=blue]
    > 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?[/color]

    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.s earch(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

    Comment

    Working...