5 回答

TA貢獻1826條經(jīng)驗 獲得超6個贊
您能做的最好的事情就是刪除 the[""和 the ]"",這樣剩下的就只有引號了。
import re
regex = r'(\[\"\"|\]\"\")'
data = r'"[""www.abccc.com"]"", "[""www.gsfa.com"]""'
print(re.sub(regex, '', data))
這給出:
"www.abccc.com", "www.gsfa.com"

TA貢獻1872條經(jīng)驗 獲得超4個贊
import re
recheck = re.compile(r'\"[\[\]]\"\"')
print(recheck.sub(' ', r'"[""www.abccc.com"]"", "[""www.gsfa.com"]""'))
應(yīng)該工作,它將匹配“,然后是[或],然后是兩個“”。
括號表示 re 應(yīng)該檢查哪些字符,所以 [""] 最終匹配一個雙引號,而 ["'] 將匹配一個字符的單引號或雙引號。這就是為什么我的 re 匹配左邊或三個雙引號內(nèi)的右括號。

TA貢獻1806條經(jīng)驗 獲得超8個贊
另一種選擇:
import regex as re
rx = re.compile(r'(?:\G(?!\A)|\[)[^]]+')
some_junky_string = '"[""www.abccc.com"]"", "[""www.gsfa.com"]""'
content = [m.group(0).strip('"') for m in rx.finditer(some_junky_string)]
print(content)

TA貢獻1824條經(jīng)驗 獲得超8個贊
你說你正在使用熊貓,所以你需要
df['col']?=?df['col'].str.replace(r'"*\["*|"*]"*',?'"')
解釋
NODE? ? ? ? ? ? ? ? ? ? ?EXPLANATION
--------------------------------------------------------------------------------
? "*? ? ? ? ? ? ? ? ? ? ? ?'"' (0 or more times (matching the most
? ? ? ? ? ? ? ? ? ? ? ? ? ?amount possible))
--------------------------------------------------------------------------------
? \[? ? ? ? ? ? ? ? ? ? ? ?'['
--------------------------------------------------------------------------------
? "*? ? ? ? ? ? ? ? ? ? ? ?'"' (0 or more times (matching the most
? ? ? ? ? ? ? ? ? ? ? ? ? ?amount possible))
--------------------------------------------------------------------------------
?|? ? ? ? ? ? ? ? ? ? ? ? OR
--------------------------------------------------------------------------------
? "*? ? ? ? ? ? ? ? ? ? ? ?'"' (0 or more times (matching the most
? ? ? ? ? ? ? ? ? ? ? ? ? ?amount possible))
--------------------------------------------------------------------------------
? ]? ? ? ? ? ? ? ? ? ? ? ? ']'
--------------------------------------------------------------------------------
? "*? ? ? ? ? ? ? ? ? ? ? ?'"' (0 or more times (matching the most
? ? ? ? ? ? ? ? ? ? ? ? ? ?amount possible))

TA貢獻1846條經(jīng)驗 獲得超7個贊
將匹配項替換為r'(\"\[\"|\"\]\")'
空字符串 ( ''
) 即可得到您要查找的輸出。
該模式使用匹配的捕獲組:
"["
字符串開頭的前導尾隨
"]"
字符串的末尾
將此作為第一個參數(shù)傳遞,re.sub(pattern, substitution, string)
將空字符串作為第二個參數(shù)傳遞,將要操作的字符串作為第三個參數(shù)傳遞,將導致用空字符串替換上面定義的匹配項 - 讓您只在開始和結(jié)束"
時根據(jù)您的原始問題生成的字符串。
下面的完整示例。
import re
result = re.sub(r'(\"\[\"|\"\]\")', '', r'"[""www.abccc.com"]""')
print(result)
output: "www.abccc.com"
添加回答
舉報