正则表达式 :查找用方括号括起来的任何字符串



我正在努力想出一个正则表达式来查找任何括在方括号中的字符串。我有一份从维基百科上抓取的大量文档,其中包含许多这样的段落:

"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".

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

这些**也在文档中吗?你只提到方括号。

如果是,正则表达式将是

(**[.*?]**)

如果它真的只是方括号,那么这与你所追求的相匹配:

([.*?])

你没有提到一种语言,但在 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)

相关内容

最新更新