如何在pyparsing中为此编写语法:匹配一组单词但不包含给定模式

时间:2009-11-26 19:26:19

标签: python pyparsing

我是Python和pyparsing的新手。我需要完成以下任务。

我的示例文字行如下:

12 items - Ironing Service    11 Mar 2009 to 10 Apr 2009
Washing service (3 Shirt)  23 Mar 2009

我需要提取项目描述,期间

tok_date_in_ddmmmyyyy = Combine(Word(nums,min=1,max=2)+ " " + Word(alphas, exact=3) + " " + Word(nums,exact=4))
tok_period = Combine((tok_date_in_ddmmmyyyy + " to " + tok_date_in_ddmmmyyyy)|tok_date_in_ddmmmyyyy)

tok_desc =  Word(alphanums+"-()") but stop before tok_period

怎么做?

2 个答案:

答案 0 :(得分:5)

我建议将SkipTo视为最适合的pyparsing类,因为你对不需要的文本有一个很好的定义,但在此之前几乎可以接受任何东西。以下是使用SkipTo的几种方法:

text = """\
12 items - Ironing Service    11 Mar 2009 to 10 Apr 2009
Washing service (3 Shirt)  23 Mar 2009"""

# using tok_period as defined in the OP

# parse each line separately
for tx in text.splitlines():
    print SkipTo(tok_period).parseString(tx)[0]

# or have pyparsing search through the whole input string using searchString
for [[td,_]] in SkipTo(tok_period,include=True).searchString(text):
    print td

两个for循环都会打印以下内容:

12 items - Ironing Service    
Washing service (3 Shirt) 

答案 1 :(得分:3)

M K Saravanan,这个特殊的解析问题并不是那么难以做好的事情:

import re
import string

text='''
12 items - Ironing Service    11 Mar 2009 to 10 Apr 2009
Washing service (3 Shirt)  23 Mar 2009
This line does not match
'''

date_pat=re.compile(
    r'(\d{1,2}\s+[a-zA-Z]{3}\s+\d{4}(?:\s+to\s+\d{1,2}\s+[a-zA-Z]{3}\s+\d{4})?)')
for line in text.splitlines():
    if line:
        try:
            description,period=map(string.strip,date_pat.split(line)[:2])
            print((description,period))
        except ValueError:
            # The line does not match
            pass

产量

# ('12 items - Ironing Service', '11 Mar 2009 to 10 Apr 2009')
# ('Washing service (3 Shirt)', '23 Mar 2009')

这里的主要工作当然是重新模式。让我们分开吧:

\d{1,2}\s+[a-zA-Z]{3}\s+\d{4}是日期的正则表达式,相当于tok_date_in_ddmmmyyyy\d{1,2}匹配一位或两位数,\s+匹配一个或多个空格,[a-zA-Z]{3}匹配3个字母等。

(?:\s+to\s+\d{1,2}\s+[a-zA-Z]{3}\s+\d{4})?是由(?:...)包围的正则表达式。 这表示非分组正则表达式。使用此,没有组(例如match.group(2))被分配给此正则表达式。这很重要,因为date_pat.split()返回一个列表,其中每个组都是列表的成员。通过抑制分组,我们将整个句点11 Mar 2009 to 10 Apr 2009保持在一起。最后的问号表示此模式可能出现零或一次。这允许正则表达式匹配两者 23 Mar 200911 Mar 2009 to 10 Apr 2009

text.splitlines()分割\n上的文字。

date_pat.split('12 items - Ironing Service 11 Mar 2009 to 10 Apr 2009')

在date_pat regexp上拆分字符串。匹配包含在返回的列表中。 因此我们得到:

['12 items - Ironing Service ', '11 Mar 2009 to 10 Apr 2009', '']

map(string.strip,date_pat.split(line)[:2])美化了结果。

如果linedate_pat不匹配,则date_pat.split(line)会返回[line,], 所以

description,period=map(string.strip,date_pat.split(line)[:2])

引发了一个ValueError,因为我们无法将只有一个元素的列表解包为2元组。我们抓住了这个例外,但只是转到下一行。

相关问题