仅返回python re.findall命令中的最终结果

时间:2014-01-21 20:02:01

标签: python search for-loop find

我正在使用re.findall来查找一系列字符串中的某些单词。

返回结果时,我只希望最后一个结果满足每个字符串中的findall任务。

我试过了:

m = re.findall(r'([0-9]+)\s([a-z]{7,})',s)

for i,j in m:     ##separate the two elemnts
    if m == i,j   ## if the total match result was equivalent to the i,j result    
                  ## then that is fine
      return i,j
    else:         ## if the total match result is not the same as i,j then I only want the 
                  ##final result to be returned
      return (    ## Not sure what to put here to only return the final result

打开完全不同的方法,以及从findall操作返回唯一或最终结果。

更新:

如下所示,我将for循环更改为:

for i in m:   返回m [-1]

这给了我想要的结果

2 个答案:

答案 0 :(得分:0)

目前还不完全清楚你要做什么,但m[-1]会返回列表中的最后一场比赛:

m = re.findall(r'([0-9]+)\s([a-z]{7,})',s)
return m[-1]

答案 1 :(得分:0)

第一个代码表示我认为你有的想法

第二个代码是Martijn提出的改进

第三和第四个代码是其他可能性

请注意,在每个代码中,我都考虑了不匹配的可能性。

import re

s1 = """abc 5895 kilimandjaro 81 little"""

s2 = """abc 145 abcdefghuj 87 short
kim 1498 witgenstein 893 sun
oh 142 stackoverflow 89 kol  871 kutr"""

print '========= 1 ============'
for s in (s1,s2,'75 moon 56 mars 10 pluto'):
    print s
    m = re.findall(r'(\d+)\s([a-z]{7,})',s)
    print m
    for i,j in m:     ##separate the two elemnts
        if m == [(i,j)]:   ## if the total match result was equivalent to the i,j result    
                      ## then that is fine
          print  '===> ',(i,j)
        else:         ## if the total match result is not the same as i,j then I only want the 
                      ##final result to be returned
          print '===> ',m[-1]    ## Not sure what to put here to only return the final result
          break
    else:
        print '===>  No match'
    print

print '========= 2 ============'
for s in (s1,s2,'75 moon 56 mars 10 pluto'):
    print s
    m = re.findall(r'(\d+)\s([a-z]{7,})',s)
    print m
    if m:
        print '===> ',m[-1]
    else:
        print '===>  No match'
    print

print '========= 3 ============'
for s in (s1,s2,'75 moon 56 mars 10 pluto'):
    print s
    for m in re.finditer(r'(\d+)\s([a-z]{7,})|^',s):
        pass
    print m.groups()   
    print

print '========= 4 ============'
for s in (s1,s2,'75 moon 56 mars 10 pluto'):
    print s
    print re.search(r'(\d+)\s([a-z]{7,})(?!.*?\d+\s[a-z]{7,})|$',s).groups()
    print

结果

========= 1 ============
abc 5895 kilimandjaro 81 little
[('5895', 'kilimandjaro')]
===>  ('5895', 'kilimandjaro')
===>  No match

abc 145 abcdefghuj 87 short
kim 1498 witgenstein 893 sun
oh 142 stackoverflow 89 kol  871 kutr
[('145', 'abcdefghuj'), ('1498', 'witgenstein'), ('142', 'stackoverflow')]
===>  ('142', 'stackoverflow')

75 moon 56 mars 10 pluto
[]
===>  No match

========= 2 ============
abc 5895 kilimandjaro 81 little
[('5895', 'kilimandjaro')]
===>  ('5895', 'kilimandjaro')

abc 145 abcdefghuj 87 short
kim 1498 witgenstein 893 sun
oh 142 stackoverflow 89 kol  871 kutr
[('145', 'abcdefghuj'), ('1498', 'witgenstein'), ('142', 'stackoverflow')]
===>  ('142', 'stackoverflow')

75 moon 56 mars 10 pluto
[]
===>  No match

========= 3 ============
abc 5895 kilimandjaro 81 little
('5895', 'kilimandjaro')

abc 145 abcdefghuj 87 short
kim 1498 witgenstein 893 sun
oh 142 stackoverflow 89 kol  871 kutr
('142', 'stackoverflow')

75 moon 56 mars 10 pluto
(None, None)

========= 4 ============
abc 5895 kilimandjaro 81 little
('5895', 'kilimandjaro')

abc 145 abcdefghuj 87 short
kim 1498 witgenstein 893 sun
oh 142 stackoverflow 89 kol  871 kutr
('145', 'abcdefghuj')

75 moon 56 mars 10 pluto
(None, None)