我需要创建一个正则表达式来捕获文本中的排序(a),(b),(c),并在它之前添加换行符,例如:
ISSSS综合方案框架的建立是为了:(a)通过加强安全部队,改善纪律和控制,为平民创造一个保护环境; (b)支持武装团体的复员和重返社会; (c)通过培训和部署国家官员(警察,监狱,司法和行政)以维护法治和公共秩序,在以前由武装团体控制的地区重建国家职能,(d)确保开放的道路通行和基础设施; (e)促进境内流离失所者和难民安全和有尊严地返回; (f)解决优先社会需求和冲突的主要根源,并开始经济复苏。
我想把它扩展到数字(1),(2),(3)......和罗马数字(I),(II),(III)...... /(i),( ⅱ),(ⅲ)
进一步的挑战,我们能否使它与任何风格相匹配,例如A-B-C-或A. B. C.或A)B)C)?
有什么想法吗?
答案 0 :(得分:1)
您可以使用带回调的sub;
import re
subject = 'The Integrated Programme Framework of the ISSSS has been established to: (a) create a protective environment for civilians by strengthening the security forces, and improving discipline and control; (b) support the demobilization and reintegration of armed groups; (c) re-establish state functions in areas formerly controlled by armed groups, through the training and deployment of state officials (police, penitentiary, judicial and administration) to uphold the rule of law and public order, (d) ensure open road access and infrastructure; (e) promote a safe and dignified return of internally displaced persons and refugees; and (f) address priority social needs and key sources of conflict and initiate economic recovery.'
def callback_f(e):
# Check your input
if e.group()[1] == 'a':
print(e.group()[1])
return '(1)'
else:
print(e.group()[1])
return '(' + e.group()[1] + ')';
result = re.sub(r'\((\w)\)', callback_f, subject)
print(result)
我不会长篇大论。但您可以使用字典自动将a/b/c/d/e
值替换为映射中匹配的值。
此演示在终端输出;
192:Desktop allendar$ python test.py
a
b
c
d
e
f
The Integrated Programme Framework of the ISSSS has been established to: A create a protective environment for civilians by strengthening the security forces, and improving discipline and control; (b) support the demobilization and reintegration of armed groups; (c) re-establish state functions in areas formerly controlled by armed groups, through the training and deployment of state officials (police, penitentiary, judicial and administration) to uphold the rule of law and public order, (d) ensure open road access and infrastructure; (e) promote a safe and dignified return of internally displaced persons and refugees; and (f) address priority social needs and key sources of conflict and initiate economic recovery.
答案 1 :(得分:1)
print re.sub("\((I+|i+|[a-z0-9])\)","\n\g<0>",buff)
将生成
The Integrated Programme Framework of the ISSSS has been established to:
(a) create a protective environment for civilians by strengthening the security forces, and improving discipline and control;
(b) support the demobilization and reintegration of armed groups;
(c) re-establish state functions in areas formerly controlled by armed groups, through the training and deployment of state officials (police, penitentiary, judicial and administration) to uphold the rule of law and public order,
(d) ensure open road access and infrastructure;
(e) promote a safe and dignified return of internally displaced persons and refugees; and
(f) address priority social needs and key sources of conflict and initiate economic recovery.
我只是因为你遇到了连字词问题,例如“重新建立”,我对制作一个完全“普遍”的问题持谨慎态度。上面的正则表达式应该涵盖你的大多数情况,你应该能够通过在'|'中添加或删除内容来轻松定制它运营商。话虽这么说,我用它玩了一点,添加了一个测试句,它运作得很好。我不得不假设我从一个空间开始,以避免一些潜在的问题,如')','。'或' - '被使用。
>>> buff += "This is a) test of i) one ii) two iii) three a. four and b- five"
>>> print re.sub(" \({0,1}(I+|i+|[a-zA-Z0-9])(\)|\.|-)","\n\g<0>",buff)
The Integrated Programme Framework of the ISSSS has been established to:
(a) create a protective environment for civilians by strengthening the security forces, and improving discipline and control;
(b) support the demobilization and reintegration of armed groups;
(c) re-establish state functions in areas formerly controlled by armed groups, through the training and deployment of state officials (police, penitentiary, judicial and administration) to uphold the rule of law and public order,
(d) ensure open road access and infrastructure;
(e) promote a safe and dignified return of internally displaced persons and refugees; and
(f) address priority social needs and key sources of conflict and initiate economic recovery.This is
a) test of
i) one
ii) two
iii) three
a. four and
b- five