2010-06-22 19 views
1

我有了套文本的文本文件,我需要提取看起来像如下:如何提取两个不同比赛之间的文字?

ITEM A blah blah blah ITEM B bloo bloo bloo ITEM A blee blee blee ITEM B 

这里是工作的代码我到目前为止:

finda = r'(Item\sA)' 
findb = r'(Item\sB)' 
match_a = re.finditer(finda, usefile, 2) # the "2" is a flag to say ignore case 
match_b = re.finditer(findb, usefile, 2) 

我知道,我可以使用span,start和end等命令来查找匹配的文本位置。但我需要做很多次所以我需要的是:

  1. 开始写在项目A,并停止在B项议题
  2. 写如果第一次迭代少于50个字符,然后丢弃和移动下一个
  3. 一旦你找到了一组与项目A开始,以项目B结束,大于50个字符写入到一个文件

由于一吨提前!我一直在旋转我的轮子。

回答

2

为什么不干脆:

with open(fname, 'w') as file: 
    for match in re.finditer(r'Item A(.+?)Item B', subject, re.I): 
     s = match.group(1) 
     if len(s) > 50: 
      file.write(s) 

注:标志的使用实际数值是re标志提供,而斜,使用。

+0

您应该使用先行断言为最终定界符允许开始和结束分隔符的重叠。 – Gumbo 2010-06-22 17:46:18

+0

谢谢!一旦我明白了这一切意味着什么,我才能使其工作。 – dandyjuan 2010-06-22 18:25:16

2

这可以在一个单一的正则表达式来完成:

with open("output.txt", "w") as f: 
    for match in re.finditer(r"(?<=Item\sA)(?:(?!Item\sB).){50,}(?=Item\sB)", subject, re.I): 
     f.write(match.group()+"\n") 

它匹配的是项目A和项目B之间还是你想匹配的分隔符,太?

正则表达式解释说:

(?<=Item\sA) # assert that we start our match right after "Item A" 
(?:   # start repeated group (non-capturing) 
    (?!Item\sB) # assert that we're not running into "Item B" 
    .   # then match any character 
){50,}   # repeat this at least 50 times 
(?=Item\sB) # then assert that "Item B" follows next (without making it part of the match) 
+0

这是很棒的代码,但它很复杂,很难弄清楚。 – vy32 2010-06-22 17:40:34

+1

@ vy32:我同意,我提供了一个自由空间版本的正则表达式来更好地解释它。 – 2010-06-22 17:45:27

相关问题