2017-05-28 79 views
-1

我很努力想出一個正則表達式來查找用方括號包住的任何字符串。 我有一個巨大的文件含有很多這樣的段落維基百科刮:正則表達式:查找任何包在方括號中的字符串

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

任何建議,將不勝感激。 謝謝!

+0

最好的建議是讓你的問題更精確,告訴我們什麼你到目前爲止已經嘗試過了,並且在編程語言和正則表達式上做了一些閱 – Jan

回答

0

文件中的那些**也是?你只提到方括號。

如果是這樣,正則表達式將

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

如果它真的只是方括號,那麼這比賽你以後:

(\[.*?\]) 

你不提語言,但在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) 
+0

謝謝!這正是我需要的。對不起,沒有提供足夠的信息。我用這個JS和它完美的工作。 – dhdz

相關問題