我正在尝试使用正则表达式从文档中解析文本。文件包含不同的结构,即1.2节、(1)节。下面的正则表达式能够解析带有小数点的文本,但无法解析()。
处理以()开头的内容的任何建议。
例如:
import re
RAW_Data = '(4) The Governor-General may arrange\n with the Chief Minister of the Australian Capital Territory for the variation or revocation of an \n\narrangement in force under subsection (3). \nNorthern Territory \n (5) The Governor-General may make arrangements with the \nAdministrator of the Northern \nTerritory with respect to the'
f = re.findall(r'(^\d+\.[\d\.]*)(.*?)(?=^\d+\.[\d\.]*)', RAW_Data,re.DOTALL|re.M|re.S)
for z in f:
z=(''.join(z).strip().replace('\n',''))
print(z)预期输出:
(4)总督可与澳洲首都领地首席部长安排更改或撤销根据第(1)款生效的安排
(3)北领地
(5)总督可就以下事宜与北领地行政长官作出安排:
发布于 2018-10-08 18:32:59
您可以尝试:
(?<=(\(\d\)|\d\.\d))(.(?!\(\d\)|\d\.\d))*要了解它是如何工作的,请考虑以下代码块:
(\(\d\)|\d\.\d)它查找(X)或X.Y类型的字符串,其中X和Y是数字。让我们称这样的字符串为“分隔符”。
现在,上面的正则表达式查找前面有分隔符的第一个字符(正向查找),并匹配后面的字符,直到找到一个后面有分隔符的字符(负向查找)。
希望它能帮上忙!
发布于 2018-10-08 18:44:35
发布于 2018-10-09 13:41:37
有一个新的RegEx \(\d\)[^(]+
\(\d\)匹配任何字符串,如(1) (2) (3) ...[^(]+匹配一个或多个字符,并在找到(时停止匹配测试平台:
但我想知道您是否有像(4) The Governor-General may arrange\n with the Chief Minister of the Austr ... (2) (3). \nNorthern Territory \n这样的特殊示例。这是来自(4) to (2)的一句话。因为我的正则表达式不能匹配这种类型的句子。
https://stackoverflow.com/questions/52700246
复制相似问题