简体   繁体   English

Python中的正则表达式替换

[英]Regular expression substitution in Python

I have a string 我有一个字符串

line = "haha (as jfeoiwf) avsrv arv (as qwefo) afneoifew"

From this I want to remove all instances of "(as...)" using some regular expression. 从这里我想删除使用一些正则表达式的"(as...)"所有实例。 I want the output to look like 我希望输出看起来像

line = "haha avsrv arv afneoifew"

I tried: 我试过了:

line = re.sub(r'\(+as .*\)','',line)

But this yields: 但这会产生:

line = "haha afneoifew"

To get non-greedy behaviour , you have to use *? 要获得非贪婪的行为 ,你必须使用*? instead of * , ie re.sub(r'\\(+as .*?\\) ','',line) . 而不是* ,即re.sub(r'\\(+as .*?\\) ','',line) To get the desired string, you also have to add a space, ie re.sub(r'\\(+as .*?\\) ','',line) . 要获得所需的字符串,还必须添加一个空格,即re.sub(r'\\(+as .*?\\) ','',line)

The problem is that your regexp matches this whole group : (as jfeoiwf) avsrv arv (as qwefo) , hence your result. 问题是你的正则表达式匹配整个组:( (as jfeoiwf) avsrv arv (as qwefo) ,因此你的结果。

You can use : 您可以使用 :

>>> import re
>>> line = "haha (as jfeoiwf) avsrv arv (as qwefo) afneoifew"
>>> line = re.sub(r'\(+as [a-zA-Z]*\)','',line)
>>> line
'haha  avsrv arv  afneoifew'

Hope it'll be helpful. 希望它会有所帮助。

You were very close. 你非常接近。 You need to use lazy quantifier '?' 你需要使用懒惰的量词'?' after .*. 之后。*。 In default it will try to capture biggest group it possibly can. 默认情况下,它会尝试捕获它可能的最大组。 With lazy quantifier it'll actually try to match smallest possible groups. 使用惰性量词,它实际上会尝试匹配最小的可能组。

line = re.sub(r'\(+as .*?\) ','',line)

尝试:

re.sub(u".\(as \w+\).", ' ',line)

尝试:

re.sub(r'\(as[^\)]*\)', '', line)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM