我正在寻找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,
答案 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]+
匹配一个或多个数字,而 *
匹配空格(如果有)。
答案 1 :(得分:0)
\d
表示正则表达式中的数字。我还在这里的数字周围添加了选项\s
。
re.sub(r'\[\s*\d+\s*\]', '', origional_string)
答案 2 :(得分:0)
>>> import re
>>> re.sub(r'\[\s*\d+\s*\]', '', original_string)