首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >正则表达式用于删除python中多余的反斜杠

正则表达式用于删除python中多余的反斜杠
EN

Stack Overflow用户
提问于 2021-04-16 23:04:00
回答 2查看 39关注 0票数 0

我在列表中列出了以下文本:

text=‘更多富勒姆开放斯特凡·约翰森,让·迈克尔·塞里和马克西姆·勒马钱德QPR热衷于富勒姆的斯特凡·约翰森,他本赛季没有进入斯科特·帕克的英超阵容;加拉塔萨雷希望让·迈克尔·塞里租借,而马克西姆·勒马钱德正在吸引前俱乐部尼斯填充2副本11周三2021年1月16:51,英国图片:皇后公园流浪者队有兴趣签下斯特凡·约翰森\n富勒姆正在听取对斯特凡·约翰森,马克西姆·勒·马钱德和让·迈克尔·塞里的报价。\n皇后公园流浪者队有兴趣签下富勒姆中场约翰森。这位挪威国脚没有入选斯科特·帕克本赛季上半场的英超阵容。30岁的约翰森自2016年从凯尔特人加盟以来,已经为俱乐部出场142次,他与克雷文农舍的合同到2022年才到期。帕克:我不欠穆里尼奥任何道歉\n这位中场在2018/19赛季下半段被租借到西布朗,但在下个赛季之前回归,并在英冠中出场33次,富勒姆通过季后赛赢得了英超联赛的晋级。图片:让·迈克尔·塞里上赛季被租借到加拉塔萨雷\n富勒姆和加拉塔萨雷之间关于租借中场让·迈克尔·塞里的可能性的谈判仍在继续。最新消息这位29岁的中场球员将第二次被租借到土耳其俱乐部,塞里上赛季曾为超级球队效力37次。塞里的合同将于2022年夏天到期,他可以选择将合同延长一年。另见:图:马克西姆·勒马钱德上赛季为富勒姆出场12次,但本赛季在斯科特·帕克的带领下只出场两次\n同时,法甲俱乐部尼斯希望后卫马克西姆·勒马钱德回到安联里维埃拉。\n富勒姆于2018年7月从法国签下勒马钱德,这位中后卫在本赛季迄今只有两次进入顶级联赛。\nLe马钱德与富勒姆的合同还有一年半的时间。\n富勒姆在英超联赛中排名第18,积4分,在足总杯对阵曼彻斯特联队(1月20日)和伯恩利(1月24日)。布莱顿(1月27日)和西布朗(1月30日)。\n跟随天空体育的一月份转会窗口\n冬季转会窗口开放到2月1日星期一晚上11点。\n关注天空体育新闻和天空体育数字平台上的所有新闻和分析,包括我们专门的转会中心博客‘

并使用以下regex并期望删除\:

代码语言:javascript
运行
复制
re.sub(r'\\\\\'',"\\'",text))

但是,输出正在清除额外的\

例如,文本包含{don\'t},使用下面的正则表达式,我打算将其转换为don't

EN

回答 2

Stack Overflow用户

发布于 2021-04-16 23:07:36

您可以只使用replace方法。如果您只想删除‘\’,则不需要使用正则表达式。显然,你需要删除'\n','\t‘等,然后你可以用它来删除额外的'\’

代码语言:javascript
运行
复制
text = text.replace('\\','')
票数 0
EN

Stack Overflow用户

发布于 2021-04-16 23:16:08

奇怪的是,我15分钟前还在查这个。我对你的文本做了几次尝试,发现了一些有用的东西。

首先,使用repr(text)将显示实际使用的反斜杠。此时,用\\替换\\\\将产生正确的字符串,就像它是用Python语言编写的一样。它基本上等同于'"' + text + '"',但在潜在的未转义语音标记方面更安全一些。

从这里开始,eval实际上会将其作为字符串处理。

代码语言:javascript
运行
复制
result = eval(repr(text).replace('\\\\', '\\'))

然后,当您打印结果时,您将得到以下结果:

代码语言:javascript
运行
复制
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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67127684

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档