簡體   English   中英

正則表達式刪除 python 中多余的反斜杠

[英]Regex to remove excess backslashes in python

我在列表中有以下文本:

text='更多富勒姆對斯蒂芬·約翰森的報價開放,讓·邁克爾·斯里和馬克西姆·勒馬爾尚 QPR 都熱衷於富勒姆的斯蒂芬·約翰森,他本賽季沒有被列入斯科特·帕克的英超大名單; 加拉塔薩雷希望讓邁克爾斯里重新租借,而馬克西姆勒馬爾尚正在吸引前俱樂部尼斯菲爾的興趣 2 Copy 11 2021 年 1 月 20 日星期三 16:51,英國 圖片:女王公園巡游者隊有興趣簽下斯蒂芬約翰森\\n富勒姆正在聽取報價對於斯蒂芬·約翰森、馬克西姆·勒馬爾尚和讓·邁克爾·斯里在一月份的轉會 window.\\n 皇后公園巡游者隊有興趣簽下富勒姆中場約翰森。 這位挪威國腳在本賽季上半賽季沒有入選斯科特·帕克的英超大名單。 \n現年 30 歲的約翰森自 2016 年從凱爾特人加盟俱樂部以來已經為俱樂部出場 142 次,他目前在 Craven Cottage 簽有合同。直到2022年。帕克:我不欠穆里尼奧一個道歉\\n這名中場球員在2018/19賽季的下半賽季被租借到西布朗,但在下個賽季之前就回來了,並在富勒姆贏得冠軍的情況下在冠軍聯賽中出場33次通過附加賽晉級英超聯賽。 圖片:讓·邁克爾·斯里上賽季在加拉塔薩雷租借 富勒姆和加拉塔薩雷之間關於租借中場球員讓·邁克爾·斯里的可能性的談判仍在繼續。 熱門最新消息\\n此舉將看到這位 29 歲的中場球員第二次租借加盟土耳其俱樂部,上賽季他效力於超級聯賽時,斯里已經為超級聯賽出場 37 次。\\nSeri\他的合同將於 2022 年夏天到期,他可以選擇將合同再延長一年。 另請參閱:圖片:馬克西姆勒馬爾尚上賽季為富勒姆出場 12 次,但本賽季到目前為止在斯科特帕克的帶領下只出場兩次\\n同時,法甲 1 俱樂部尼斯希望后衛馬克西姆勒馬爾尚重返安聯里維埃拉。\\ n 富勒姆於 2018 年 7 月從法甲簽下了勒馬爾尚,而這位中后衛本賽季迄今為止只在頂級聯賽中出場過兩次。\\n勒馬爾尚在富勒姆的合同還剩一年半。\\恩富勒姆在英超聯賽中排名第 18 位,距離安全區還有 4 分,他們將在 1 月 20 日對陣曼聯、1 月 24 日在足總杯中對陣伯恩利、1 月 27 日在布萊頓和 1 月 30 日對陣西布朗。 \\n關注天空體育的一月轉會 window \\n冬季轉會 window 開放至 2 月 1 日星期一晚上 11 點。\\n關注天空體育新聞和天空體育數字平台上的所有新聞和分析,包括我們專門的轉移中心博客'

並使用以下正則表達式並期望刪除 \:

re.sub(r'\\\\\'',"\\'",text))

但是,output 正在清理額外的 \

例如,文本有 {don\\'t} 並且使用以下正則表達式我打算將其轉換為不

你可以只使用替換方法。 如果您只想刪除“\”,則無需使用正則表達式。 顯然,你需要刪除'\n','\t'等,然后你可以使用它來刪除多余的'\'

text = text.replace('\\','')

很奇怪,我 15 分鍾前才剛剛查到這個。 我對你的文字進行了幾次嘗試,發現了一些有用的東西。

首先,使用repr(text)將顯示實際使用的反斜杠。 此時將\\\\替換為\\將產生正確的字符串,就好像它是用 Python 編寫的一樣。 它基本上相當於'"' + text + '"' ,但就潛在的未轉義語音標記而言更安全一些。

從這里開始, eval實際上會將其作為字符串處理。

result = eval(repr(text).replace('\\\\', '\\'))

然后,當您打印結果時,您會得到以下結果:

More Fulham open to offers for Stefan Johansen, Jean Michael Seri and Maxime Le Marchand QPR are keen on Fulham's Stefan Johansen who was not included in Scott Parker's Premier League squad this season; Galatasaray want Jean Michael Seri back on loan while Maxime Le Marchand is attracting interest from former club Nice Fill 2 Copy 11 Wednesday 20 January 2021 16:51, UK Image: Queens Park Rangers are interested in signing Stefan Johansen
Fulham are listening to offers for Stefan Johansen, Maxime Le Marchand and Jean Michael Seri during the January transfer window.
Queens Park Rangers are interested in signing Fulham midfielder Johansen. The Norwegian international was not included in Scott Parker's Premier League squad for the first half of this season.
Johansen, 30, has made 142 appearances for the club since arriving from Celtic in 2016, he is under contract at Craven Cottage until 2022. Parker: I don't owe Mourinho an apology
The midfielder spent the second half of the 2018/19 campaign on loan at West Brom but returned ahead of the following season and played 33 times in the Championship as Fulham won promotion back to the Premier League via the play-offs. Image: Jean Michael Seri spent last season on loan at Galatasaray
Talks continue between Fulham and Galatasaray over the possibility of a loan deal for midfielder Jean Michael Seri. Trending Latest News
A move would see the 29-year-old central midfielder join the Turkish club on loan for a second time, with Seri having played 37 times for the Super Lig side while he was there last season.
Seri's contract expires in the summer of 2022, he has the option to extend the deal by a further year. Also See: Image: Maxime Le Marchand played 12 times for Fulham last season but has made just two appearances under Scott Parker so far this term
Meanwhile, Ligue 1 club Nice want defender Maxime Le Marchand to return to the Allianz Riviera.
Fulham signed Le Marchand from the French side in July 2018 and the centre-back has featured just twice in the top flight so far this season.
Le Marchand also has a year and a half remaining on his current contract at Fulham.
Fulham, who are 18th and four points from safety in the Premier League, play Manchester United (January 20), Burnley in the FA Cup (January 24), Brighton (January 27), and West Brom (January 30) before the end of the month.
Follow the January transfer window with Sky Sports
The winter transfer window is open until Monday, February 1 at 11pm.
Follow all the news and analysis on Sky Sports News and across Sky Sports ' digital platforms, including our dedicated Transfer Centre blog

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM