通过索引与re.finditer循环匹配

时间:2013-04-19 17:09:05

标签: python regex

我想知道如何通过索引导航finditer正则表达式操作生成的对象。

我的字符串是s = "fish oil X22 stack peanut C4"

这是我的代码:

import re
words = re.finditer('\S+', s)
has_digits = re.compile(r'\d').search
for word in words:
    if has_digits(word.group()):
        print (the word that is two words back)

期望输出=

fish
stack

2 个答案:

答案 0 :(得分:4)

您可以使用deque来保存元素。然后这变得容易:

import re
from collections import deque
s = 'fish oil X22 stack peanut C4'
words = re.finditer('\S+', s)
has_digits = re.compile(r'\d').search
deq = deque([],2)
for word in words:
    wordtxt = word.group()
    if has_digits(wordtxt):
        print (deq[0])
    deq.append(wordtxt)

有点不清楚字符串会发生什么:

s = 'fish oil X22 stack C4'

它应该打印" fish"和"石油"或者" fish"和" X22"。另外,如果第一个子串是" X22&#34 ;?在我的回答中,这将导致IndexError,但很难知道你想用它做什么...

答案 1 :(得分:1)

您可以使用itertools.teeitertools.izip

import re
import itertools as it

s = "fish oil X22 stack peanut C4"
words = re.finditer('\S+', s)
has_digits = re.compile(r'\d').search
words, words_copy = it.tee(words)
next(words); next(words)       #Skip the first two words of one iterator
for word, back_word in it.izip(words, words_copy):
    if has_digits(word.group()):
            print(back_word.group())