我正在尝试根据经常出现的字符串将段落解析为数据字段。这是一个例子:
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 字符序列。
使用像这样的正则表达式
([Ww]ork will be performed (?:in|at)|[Ll]ocations? of performance (?:is|are))\s*(.*?)( and (the work )?is expected| with a(.*)completion date)
参见 regex demo
