我有一个维基百科的转储文件,但是无法找到适当的正则表达式模式来删除表达式中的双方括号。以下是表达式的示例:
line = 'is the combination of the code names for Herbicide Orange (HO) and Agent LNX, one of the [[herbicide]]s and [[defoliant]]s used by the [[United States armed forces|U.S. military]] as part of its [[herbicidal warfare]] program, [[Operation Ranch Hand]], during the [[Vietnam War]] from 1961 to 1971.'
我想删除所有带有以下条件的方括号:
如果方括号内没有竖线分隔符,则删除方括号。
例如:
[[herbicide]]s
变成herbicides
。如果方括号内有竖线分隔符,则删除方括号并仅使用分隔符后面的短语。
例如:
[[United States armed forces|U.S. military]]
变成U.S. military
。
我尝试使用 re.match
和 re.search
,但未能得到所需的输出结果。
谢谢你的帮助!
\\1
,否则它会打印一个八进制字符。 - Nick Garvey\\1
的表达式。我使用Python 2.7并且出现以下错误:raise error, v # invalid expression sre_constants.error: nothing to repeat
不知道这是为什么? - notrockstar