我正在努力想出一个正则表达式来查找任何用方括号括起来的字符串。\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]**".\nRun 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".\nRun Code Online (Sandbox Code Playgroud)\n\n如有任何建议,我们将不胜感激。\n谢谢!
\n**文件里也有这些吗?您只提到了方括号。
如果是的话,正则表达式将是
\n\n(\\*\\*\\[.*?\\]\\*\\*)\nRun Code Online (Sandbox Code Playgroud)\n\n如果它实际上只是方括号,那么这与您所追求的相匹配:
\n\n(\\[.*?\\])\nRun Code Online (Sandbox Code Playgroud)\n\n你没有提到一种语言,但在 Python 中,这可以通过以下方式完成
\n\nimport 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)\nRun Code Online (Sandbox Code Playgroud)\n
| 归档时间: |
|
| 查看次数: |
1688 次 |
| 最近记录: |