我试图根据常见的字符串将段落解析为数据字段。举个例子:
tstStr = 'Locations of performance are California, North Carolina and Pennsylvania, with a Sept. 14, 2017, performance completion date.'
pperf = '([Ww]ork will be performed [(in)(at)]|[Ll]ocation[(s )\s] of performance [(is)(are)])(.*?)( and (the work )?is expected| with a(.*)completion date)'
pTest = re.search(pperf, tstStr)
预期结果是:
pTest.group(2)
California, North Carolina and Pennsylvania,
相反,我得到:
pTest.group(2)
re California, North Carolina and Pennsylvania,
第一组怎么写得不对
谢谢你
关键是你的
[(is)(are)]
实际上是一个character class匹配的1个符号,(
,i
,s
,等等。你需要一个非捕获组,(?:is|are)
匹配is
或are
字符序列使用正则表达式
参见regex demo
相关问题 更多 >
编程相关推荐