簡體   English   中英

如何在Python中搜索正則表達式匹配?

[英]How do I search through regex matches in Python?

我需要嘗試一個字符串對多個(獨占 - 意味着匹配其中一個的字符串不能匹配任何其他)正則表達式,並根據它匹配的代碼執行不同的代碼。 我現在擁有的是:

m = firstre.match(str)
if m:
    # Do something

m = secondre.match(str)
if m:
    # Do something else

m = thirdre.match(str)
if m:
    # Do something different from both

除了丑陋之外,這個代碼與所有正則表達式匹配,即使它匹配其中一個(比如firstre),這是低效的。 我試着用:

elif m = secondre.match(str)

但是我知道if語句中不允許賦值。

有沒有一種優雅的方式來實現我想要的?

def doit( s ):

    # with some side-effect on a
    a = [] 

    def f1( s, m ):
        a.append( 1 )
        print 'f1', a, s, m

    def f2( s, m ):
        a.append( 2 )
        print 'f2', a, s, m

    def f3( s, m ):
        a.append( 3 )
        print 'f3', a, s, m

    re1 = re.compile( 'one' )
    re2 = re.compile( 'two' )
    re3 = re.compile( 'three' )


    func_re_list = (
        ( f1, re1 ), 
        ( f2, re2 ), 
        ( f3, re3 ),
    )
    for myfunc, myre in func_re_list:
        m = myre.match( s )
        if m:
            myfunc( s, m )
            break


doit( 'one' ) 
doit( 'two' ) 
doit( 'three' ) 

這可能有點過於設計解決方案,但您可以將它們組合為具有命名組的單個正則表達式,並查看匹配的組。 這可以封裝為輔助類:

import re
class MultiRe(object):
    def __init__(self, **regexps):
        self.keys = regexps.keys()
        self.union_re = re.compile("|".join("(?P<%s>%s)" % kv for kv in regexps.items()))

    def match(self, string, *args):
        result = self.union_re.match(string, *args)
        if result:
            for key in self.keys:
                if result.group(key) is not None:
                    return key

查找將是這樣的:

multi_re = MultiRe(foo='fo+', bar='ba+r', baz='ba+z')
match = multi_re.match('baaz')
if match == 'foo':
     # one thing
elif match == 'bar':
     # some other thing
elif match == 'baz':
     # or this
else:
     # no match

對於未記錄但非常有用的re.Scanner類,這是一個很好的應用程序。

一些想法,其中沒有一個是好的,但它可能適合您的代碼:

如何將代碼放在一個單獨的函數中,即MatchRegex() ,它返回匹配的正則表達式。 這樣,在函數內部,您可以在匹配第一個(或第二個)正則表達式后使用返回,這意味着您將失去低效率。

當然,你總是可以使用嵌套的if語句:

m = firstre.match(str)
if m:
   # Do something
else:
    m = secondre.match(str)
    ...

我真的沒有任何理由不去嵌套if s。 它們非常容易理解,並且您可以隨心所欲地使用它們。 我只是為了他們的簡單而去找他們。

你可以用

def do_first(str, res, actions):
  for re,action in zip(res, actions):
    m = re.match(str)
    if m:
      action(str)
      return

所以,例如,假設您已定義

def do_something_1(str):
  print "#1: %s" % str

def do_something_2(str):
  print "#2: %s" % str

def do_something_3(str):
  print "#3: %s" % str

firstre  = re.compile("foo")
secondre = re.compile("bar")
thirdre  = re.compile("baz")

然后用它來調用它

do_first("baz",
         [firstre,        secondre,       thirdre],
         [do_something_1, do_something_2, do_something_3])

也許是早期回歸?

def doit(s):
    m = re1.match(s)
    if m:
        # Do something
        return

    m = re2.match(s)
    if m:
        # Do something else
        return

    ...

螞蟻Aasma的答案也很好。 如果您更喜歡腳手架,可以使用詳細的正則表達式語法自行編寫。

re = re.compile(r'''(?x)    # set the verbose flag
    (?P<foo> fo+ )
  | (?P<bar> ba+r )
  | #...other alternatives...
''')

def doit(s):
    m = re.match(s)
    if m.group('foo'):
        # Do something
    elif m.group('bar'):
        # Do something else
    ...

我做了很多。 它很快,它與re.finditer一起re.finditer

如果您只需要正則表達式匹配的真/假,請使用elif:

if regex1.match(str):
    # do stuff
elif regex2.match(str):
    # and so on

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM