[英]Regex optional match in python fails
tickettypepat = (r'MIS Notes:.*(//p//)?.*')
retype = re.search(tickettypepat,line)
if retype:
print retype.group(0)
print retype.group(1)
给定输入。
MIS Notes: //p//
谁能告诉我为什么group(0)是
MIS Notes: //p//
和组(1)返回为无?
我最初使用正则表达式是因为,在遇到问题之前,匹配要比仅匹配// p //要复杂得多,这是完整的代码。 我在这方面还很陌生,所以请原谅我,我敢肯定,有很多更好的方法可以完成大部分任务,而且如果有人想指出这些建议,那将是很棒的。 但是,除了// [pewPEW] //的正则表达式存在问题之外,它似乎也很有效。 感谢您的帮助。
filename = (r'.\4-12_4-26.txt')
import re
import sys
#Clean up output from the web to ensure that you have one catagory per line
f = open(filename)
w = open('cleantext.txt','w')
origdatepat = (r'(Ticket Date: )([0-9]+/[0-9]+/[0-9]+),( [0-9]+:[0-9]+ [PA]M)')
tickettypepat = (r'MIS Notes:.*(//[pewPEW]//)?.*')
print 'Begining Blank Line Removal'
for line in f:
redate = re.search(origdatepat,line)
retype = re.search(tickettypepat,line)
if line == ' \n':
line = ''
print 'Removing blank Line'
#remove ',' from time and date line
elif redate:
line = redate.group(1) + redate.group(2)+ redate.group(3)+'\n'
print 'Redating... ' + line
elif retype:
print retype.group(0)
print retype.group(1)
if retype.group(1) == '//p//':
line = line + 'Type: Phone\n'
print 'Setting type for... ' + line
elif retype.group(1) == '//e//':
line = line + 'Type: Email\n'
print 'Setting type for... ' + line
elif retype.group(1) == '//w//':
line = line + 'Type: Walk-in\n'
print 'Setting type for... ' + line
elif retype.group(1) == ('' or None):
line = line + 'Type: Ticket\n'
print 'Setting type for... ' + line
w.write(line)
print 'Closing Files'
f.close()
w.close()
这是一些示例输入。
Ticket No.: 20100426132
Ticket Date: 04/26/10, 10:22 AM
Close Date:
Primary User: XXX
Branch: XXX
Help Tech: XXX
Status: Pending
Priority: Medium
Application: xxx
Description: some issue
Resolution: some resolution
MIS Notes: some random stuff //p// followed by more stuff
Key Words:
Ticket No.: 20100426132
Ticket Date: 04/26/10, 10:22 AM
Close Date:
Primary User: XXX
Branch: XXX
Help Tech: XXX
Status: Pending
Priority: Medium
Application: xxx
Description: some issue
Resolution: some resolution
MIS Notes: //p//
Key Words:
Ticket No.: 20100426132
Ticket Date: 04/26/10, 10:22 AM
Close Date:
Primary User: XXX
Branch: XXX
Help Tech: XXX
Status: Pending
Priority: Medium
Application: xxx
Description: some issue
Resolution: some resolution
MIS Notes: //e// stuff....
Key Words:
Ticket No.: 20100426132
Ticket Date: 04/26/10, 10:22 AM
Close Date:
Primary User: XXX
Branch: XXX
Help Tech: XXX
Status: Pending
Priority: Medium
Application: xxx
Description: some issue
Resolution: some resolution
MIS Notes:
Key Words:
MIS Notes:.*(//p//)?.*
工作方式与此类似,以"MIS Notes: //p//"
为目标:
MIS Notes:
与"MIS Notes:"
匹配,在此不感到惊讶。 .*
立即运行到字符串的末尾(到目前为止匹配"MIS Notes: //p//"
) (//p//)?
是可选的。 什么都没发生。 .*
没有任何可匹配的内容,我们已经在字符串的末尾。 由于星号允许前一个原子的匹配为零,因此正则表达式引擎停止将整个字符串报告为匹配项,并将子组报告为空。 现在,当您将正则表达式更改为MIS Notes:.*(//p//).*
,行为将更改:
MIS Notes:
匹配"MIS Notes:"
,这里仍然没有任何惊喜。 .*
立即运行到字符串的末尾(到目前为止匹配"MIS Notes: //p//"
) (//p//)
是必需的。 为了满足此要求,引擎开始逐个字符地回溯。 (到目前为止,匹配"MIS Notes: "
) (//p//)
可以匹配。 子组1被保存并包含"//p//"
。 .*
运行到字符串的末尾。 提示:如果您对匹配的内容不感兴趣,那就多余了,可以将其删除。 现在,当您将正则表达式更改为MIS Notes:.*?//(p)//
,行为将再次更改:
MIS Notes:
与"MIS Notes:"
匹配,这里仍然没有任何惊喜。 .*?
为非贪婪,并在继续进行之前检查以下原子(到目前为止匹配"MIS Notes: "
) //(p)//
可以匹配。 子组1被保存并包含"p"
。 现在,如果您知道//p//
之前不能有/
,则可以使用: MIS Notes:[^/]*//(p)//
:
MIS Notes:
与"MIS Notes:"
匹配,您就会明白。 [^/]*
可以快进到第一个斜杠(比.*?
快) //(p)//
可以匹配。 子组1被保存并包含"p"
。 正则表达式是贪婪的,这意味着.*
尽可能匹配整个字符串。 因此,没有什么可与可选组匹配的。 group(0)
始终是整个匹配字符串。
根据您的评论,您为什么要使用正则表达式? 这样还不够吗:
if line.startswith('MIS Notes:'): # starts with that string
data = line[len('MIS Notes:'):] # the rest in the interesting part
if '//p//' in data:
stuff, sep, rest = data.partition('//p//') # or sothing like that
else:
pass #other stuff
该模式对于您的目的是不明确的。 最好用前缀或后缀对它们进行分组。 在这里的示例中,我选择了前缀分组。 基本上,如果//p//
出现在行中,则前缀为非空。 后缀是//p//
项之后的所有内容,如果行中不存在,则为行中的所有内容。
import re
lines = ['MIS Notes: //p//',
'MIS Notes: prefix//p//suffix']
tickettypepat = (r'MIS Notes: (?:(.*)//p//)?(.*)')
for line in lines:
m = re.search(tickettypepat,line)
print 'line:', line
if m: print 'groups:', m.groups()
else: print 'groups:', m
结果:
line: MIS Notes: //p//
groups: ('', '')
line: MIS Notes: prefix//p//suffix
groups: ('prefix', 'suffix')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.