MK Saravanan、この特定の構文解析の問題は、良い 'ole re:
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')
ここでの主力はもちろん re パターンです。それを分解しましょう:
\d{1,2}\s+[a-zA-Z]{3}\s+\d{4}
は日付の正規表現で、 と同等ですtok_date_in_ddmmmyyyy
。\d{1,2}
1 つまたは 2 つの数字に\s+
一致する、1 つ以上の空白に[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
をまとめています。最後のクエスチョン マークは、このパターンが 0 回または 1 回発生する可能性があることを示します。これにより、正規表現が と の両方に一致
23 Mar 2009
し11 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 正規表現で文字列を分割します。返されたリストに一致が含まれます。したがって、次のようになります。
['12 items - Ironing Service ', '11 Mar 2009 to 10 Apr 2009', '']
map(string.strip,date_pat.split(line)[:2])
結果を美しくします。
line
一致しない場合はdate_pat
をdate_pat.split(line)
返す[line,]
ので、
description,period=map(string.strip,date_pat.split(line)[:2])
要素が 1 つしかないリストを 2 タプルにアンパックできないため、ValueError が発生します。この例外をキャッチしますが、単に次の行に渡します。