Python正则表达式删除所有出现的[Number]

时间:2014-03-10 17:50:07

标签: python regex

我正在寻找python中的正则表达式语句,它将从字符串中删除[1]或[17]或基本上[*]的所有出现。出现如下*其中*等于某个数字:

[*]
[ * ]
[ *]
[* ]

目前我有:

re.sub(r'\[*\]', '', origional_string)

提出invalid expression

示例输入字符串:

makeup of organisms.[10] In 1997, while working at the University of Tennessee, Pigliucci received the Theodosius Dobzhansky Prize,[11] 

预期产出:

makeup of organisms. In 1997, while working at the University of Tennessee, Pigliucci received the Theodosius Dobzhansky Prize,

3 个答案:

答案 0 :(得分:1)

我想这样的事情应该有效:

import re

origional_string = "makeup of organisms.[10] In 1997, while working at the University of Tennessee, Pigliucci received the Theodosius Dobzhansky Prize,[11]"

result = re.sub(r'\[ *[0-9]+ *\]', '', origional_string)

print(result)

[0-9]+匹配一个或多个数字,而 *匹配空格(如果有)。

ideone demo

答案 1 :(得分:0)

\d表示正则表达式中的数字。我还在这里的数字周围添加了选项\s

re.sub(r'\[\s*\d+\s*\]', '', origional_string)

答案 2 :(得分:0)

 >>> import re
 >>> re.sub(r'\[\s*\d+\s*\]', '', original_string)