如何将这个正则表达式习语从Perl翻译成Python?

前端之家收集整理的这篇文章主要介绍了如何将这个正则表达式习语从Perl翻译成Python?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
大约一年前我从Perl切换到Python,并没有回头。只有一个成语,我发现我可以在Perl比在Python更容易做到:
if ($var =~ /foo(.+)/) {
  # do something with $1
} elsif ($var =~ /bar(.+)/) {
  # do something with $1
} elsif ($var =~ /baz(.+)/) {
  # do something with $1
}

相应的Python代码不那么优雅,因为if语句继续嵌套:

m = re.search(r'foo(.+)',var)
if m:
  # do something with m.group(1)
else:
  m = re.search(r'bar(.+)',var)
  if m:
    # do something with m.group(1)
  else:
    m = re.search(r'baz(.+)',var)
    if m:
      # do something with m.group(2)

有没有人有一个优雅的方式来重现这种模式在Python?我看过使用的匿名函数调度表,但是对于少量的正则表达式,这些表似乎对我来说是不方便的。

r"""
This is an extension of the re module. It stores the last successful
match object and lets you access it's methods and attributes via
this module.

This module exports the following additional functions:
    expand  Return the string obtained by doing backslash substitution on a
            template string.
    group   Returns one or more subgroups of the match.
    groups  Return a tuple containing all the subgroups of the match.
    start   Return the indices of the start of the substring matched by
            group.
    end     Return the indices of the end of the substring matched by group.
    span    Returns a 2-tuple of (start(),end()) of the substring matched
            by group.

This module defines the following additional public attributes:
    pos         The value of pos which was passed to the search() or match()
                method.
    endpos      The value of endpos which was passed to the search() or
                match() method.
    lastindex   The integer index of the last matched capturing group.
    lastgroup   The name of the last matched capturing group.
    re          The regular expression object which as passed to search() or
                match().
    string      The string passed to match() or search().
"""

import re as re_

from re import *
from functools import wraps

__all__ = re_.__all__ + [ "expand","group","groups","start","end","span","last_match","pos","endpos","lastindex","lastgroup","re","string" ]

last_match = pos = endpos = lastindex = lastgroup = re = string = None

def _set_match(match=None):
    global last_match,pos,endpos,lastindex,lastgroup,re,string
    if match is not None:
        last_match = match
        pos = match.pos
        endpos = match.endpos
        lastindex = match.lastindex
        lastgroup = match.lastgroup
        re = match.re
        string = match.string
    return match

@wraps(re_.match)
def match(pattern,string,flags=0):
    return _set_match(re_.match(pattern,flags))


@wraps(re_.search)
def search(pattern,flags=0):
    return _set_match(re_.search(pattern,flags))

@wraps(re_.findall)
def findall(pattern,flags=0):
    matches = re_.findall(pattern,flags)
    if matches:
        _set_match(matches[-1])
    return matches

@wraps(re_.finditer)
def finditer(pattern,flags=0):
    for match in re_.finditer(pattern,flags):
        yield _set_match(match)

def expand(template):
    if last_match is None:
        raise TypeError,"No successful match yet."
    return last_match.expand(template)

def group(*indices):
    if last_match is None:
        raise TypeError,"No successful match yet."
    return last_match.group(*indices)

def groups(default=None):
    if last_match is None:
        raise TypeError,"No successful match yet."
    return last_match.groups(default)

def groupdict(default=None):
    if last_match is None:
        raise TypeError,"No successful match yet."
    return last_match.groupdict(default)

def start(group=0):
    if last_match is None:
        raise TypeError,"No successful match yet."
    return last_match.start(group)

def end(group=0):
    if last_match is None:
        raise TypeError,"No successful match yet."
    return last_match.end(group)

def span(group=0):
    if last_match is None:
        raise TypeError,"No successful match yet."
    return last_match.span(group)

del wraps  # Not needed past module compilation

例如:

if gre.match("foo(.+)",var):
  # do something with gre.group(1)
elif gre.match("bar(.+)",var):
  # do something with gre.group(1)
elif gre.match("baz(.+)",var):
  # do something with gre.group(1)

猜你在找的正则表达式相关文章