Python regular expression expansion

I have a line

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

From this, I want to remove all instances "(as...)"using some regular expression. I want the result to look like

line = "haha avsrv arv afneoifew"

I tried:

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

But it gives:

line = "haha afneoifew"

+4
source share
5 answers

To get inanimate behavior you should use *?instead *, i.e. re.sub(r'\(+as .*?\) ','',line). To get the desired line, you also need to add a space, i.e. re.sub(r'\(+as .*?\) ','',line).

+4
source

The problem is that your regular expression matches this entire group: (as jfeoiwf) avsrv arv (as qwefo)therefore, your result.

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'

, .

+2

You were very close. Do you need to use a lazy quantifier? after.*. By default, he will try to capture the largest group that is possible. With a lazy quantifier, he will try to match the smallest possible groups.

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

Try:

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

to try:

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

All Articles