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

dhd*_*hdz 1 regex

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

\n\n
"Theology translates into English from the Greek theologia (\xce\xb8\xce\xb5\xce\xbf\xce\xbb\xce\xbf\xce\xb3\xce\xaf\xce\xb1) which derived from \xce\xa4heos (\xce\x98\xce\xb5\xcf\x8c\xcf\x82), meaning "God," and -logia (-\xce\xbb\xce\xbf\xce\xb3\xce\xaf\xce\xb1),**[12]** meaning "utterances, sayings, or oracles" (a word related to logos **[\xce\xbb\xcf\x8c\xce\xb3\xce\xbf\xcf\x82][Citation needed]**".\n
Run Code Online (Sandbox Code Playgroud)\n\n

期望的结果是:

\n\n
"Theology translates into English from the Greek theologia (\xce\xb8\xce\xb5\xce\xbf\xce\xbb\xce\xbf\xce\xb3\xce\xaf\xce\xb1) which derived from \xce\xa4heos (\xce\x98\xce\xb5\xcf\x8c\xcf\x82), meaning "God," and -logia (-\xce\xbb\xce\xbf\xce\xb3\xce\xaf\xce\xb1), meaning "utterances, sayings, or oracles" (a word related to logos".\n
Run Code Online (Sandbox Code Playgroud)\n\n

如有任何建议,我们将不胜感激。\n谢谢!

\n

Ped*_*sta 5

**文件里也有这些吗?您只提到了方括号。

\n\n

如果是的话,正则表达式将是

\n\n
(\\*\\*\\[.*?\\]\\*\\*)\n
Run Code Online (Sandbox Code Playgroud)\n\n

如果它实际上只是方括号,那么这与您所追求的相匹配:

\n\n
(\\[.*?\\])\n
Run Code Online (Sandbox Code Playgroud)\n\n

你没有提到一种语言,但在 Python 中,这可以通过以下方式完成

\n\n
import re\n\nmy_re = r\'(\\*\\*\\[.*?\\]\\*\\*)\'\nmy_string = \'"Theology translates into English from the Greek theologia (\xce\xb8\xce\xb5\xce\xbf\xce\xbb\xce\xbf\xce\xb3\xce\xaf\xce\xb1) which derived from \xce\xa4heos (\xce\x98\xce\xb5\xcf\x8c\xcf\x82), meaning "God," and -logia (-\xce\xbb\xce\xbf\xce\xb3\xce\xaf\xce\xb1),**[12]** meaning "utterances, sayings, or oracles" (a word related to logos **[\xce\xbb\xcf\x8c\xce\xb3\xce\xbf\xcf\x82][Citation needed]**".\'\nmy_corrected_string = re.sub(my_re, \'\', my_string)\n\nprint(my_corrected_string)\n
Run Code Online (Sandbox Code Playgroud)\n