2

我需要创建一个正则表达式来捕获文本中的排序(a)、(b)、(c),并在它之前添加一个换行符,例如:

ISSSS 的综合计划框架旨在: (a) 通过加强安全部队、改善纪律和控制,为平民创造保护环境;(b) 支持武装团体的复员和重返社会;(c) 通过培训和部署国家官员(警察、监狱、司法和行政部门),在以前由武装团体控制的地区重建国家职能,以维护法治和公共秩序,(d) 确保道路畅通和基础设施;(e) 促进境内流离失所者和难民安全和有尊严地返回;(f) 解决优先的社会需求和冲突的主要根源,并启动经济复苏。

我想把它扩展到数字 (1),(2),(3)... 和罗马数字 (I),(II),(III).../ (i),(ii), ㈢

进一步的挑战,我们能否让它匹配任何样式,例如 A- B- C- 或 ABC 或 A) B) C) ?

有任何想法吗?

4

2 回答 2

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
于 2013-04-10T18:07:37.370 回答
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.
于 2013-04-10T18:05:39.933 回答