正则表达式:查找包含在方括号中的任何字符串

时间:2017-05-28 14:19:57

标签: regex

我正在努力想出一个正则表达式,找到包含在方括号中的任何字符串。 我从维基百科中删除了一个包含许多段落的大量文档:

"Theology translates into English from the Greek theologia (θεολογία) which derived from Τheos (Θεός), meaning "God," and -logia (-λογία),**[12]** meaning "utterances, sayings, or oracles" (a word related to logos **[λόγος][Citation needed]**".

期望的结果是:

"Theology translates into English from the Greek theologia (θεολογία) which derived from Τheos (Θεός), meaning "God," and -logia (-λογία), meaning "utterances, sayings, or oracles" (a word related to logos".

任何建议都将不胜感激。 谢谢!

1 个答案:

答案 0 :(得分:1)

文档中的**也是吗?你只提方括号。

如果是,那么正则表达式将是

(\*\*\[.*?\]\*\*)

如果它只是方括号,那么这与你之后的情况相符:

(\[.*?\])

你没有提到一种语言,但在Python中,这将由

完成
import re

my_re = r'(\*\*\[.*?\]\*\*)'
my_string = '"Theology translates into English from the Greek theologia (θεολογία) which derived from Τheos (Θεός), meaning "God," and -logia (-λογία),**[12]** meaning "utterances, sayings, or oracles" (a word related to logos **[λόγος][Citation needed]**".'
my_corrected_string = re.sub(my_re, '', my_string)

print(my_corrected_string)