我试图删除所有不同格式的项目符号,这基本上是我的情况:
c.2低于国际贫困线的就业人口,按性别和年龄分列(%)年龄:15岁以上责任低于国际贫困线的就业人口,按性别和年龄分列(%)年龄:15岁以上. 1低于国际贫困线的就业人口,按性别和年龄分列(%)年龄:15岁以上
- 低于国际贫困线的就业人口,按性别和年龄分列(%)年龄:15岁以上1.2低于国际贫困线的就业人口,按性别和年龄分列(%)年龄:15岁以上1.1.1低于国际贫困线的就业人口,按性别和年龄分列(%)年龄:15岁以上5.6.2 (S.1.C.1)低于国际贫困线的就业人口,按性别和年龄分列(%)年龄:15岁以上5.6.2 (S.3)低于国际贫困线的就业人口,按性别和年龄分列(%)年龄:15岁以上5.6.2 (S.4.C.13)低于国际贫困线的就业人口,按性别和年龄分列(%)年龄:15岁以上
我想要一个正则表达式来删除项目符号,无论他们是什么形式,只有:就业人口低于国际贫困线,按性别和年龄(%)年龄:15+
我试图使用^(?:d+.)+d*s*
,它工作得很好,但它只检测1。或者1.2或1.1.1,这是我想要的开始,所以它是正确的,但现在我的给定更改为这个。
提前谢谢你,旁注:我使用python3
^[a-zd+].(d+)?.?(d+)?(s(.*)s)?s+
这是捕捉所有类型的项目符号在你的例子,这是证明:https://regex101.com/r/sj4PgN/2
可以使用
^(?:[a-z]|d+)(?:.d+)*.?s*(?:([^()]*)s*)?
^
字符串起始(?:[a-z]|d+)
匹配字符a-z或匹配1+数字(?:.d+)*
可选重复.
和1+数字.?
匹配可选点s*
匹配可选的空白字符(?:([^()]*)s*)?
可选匹配部分(...)
,后跟最佳空格
Regex演示
在替换中使用空字符串
如果圆括号之间的部分是给定的特定模式,即大写字符a - z后跟一个点和数字:
^(?:[a-z]|d+)(?:.d+)*.?s*(?:([A-Z].d+(?:.[A-Z].d+)*)s*)?
Regex演示
例子import re
pattern = r"^(?:[a-z]|d+)(?:.d+)*.?s*(?:([^()]*)s*)?"
s = ("c.2 Employed population below international poverty line, by sex and age (%) Age: 15+n"
"b.1 Employed population below international poverty line, by sex and age (%) Age: 15+n"
"a.1 Employed population below international poverty line, by sex and age (%) Age: 15+n"
"1. Employed population below international poverty line, by sex and age (%) Age: 15+n"
"1.2 Employed population below international poverty line, by sex and age (%) Age: 15+ n"
"1.1.1 Employed population below international poverty line, by sex and age (%) Age: 15+ n"
"5.6.2 (S.1.C.1) Employed population below international poverty line, by sex and age (%) Age: 15+n"
"5.6.2 (S.3) Employed population below international poverty line, by sex and age (%) Age: 15+n"
"5.6.2 (S.4.C.13) Employed population below international poverty line, by sex and age (%) Age: 15+")
result = re.sub(pattern, "", s, 0, re.MULTILINE)
if result:
print(result)
输出Employed population below international poverty line, by sex and age (%) Age: 15+
Employed population below international poverty line, by sex and age (%) Age: 15+
Employed population below international poverty line, by sex and age (%) Age: 15+
Employed population below international poverty line, by sex and age (%) Age: 15+
Employed population below international poverty line, by sex and age (%) Age: 15+
Employed population below international poverty line, by sex and age (%) Age: 15+
Employed population below international poverty line, by sex and age (%) Age: 15+
Employed population below international poverty line, by sex and age (%) Age: 15+
Employed population below international poverty line, by sex and age (%) Age: 15+