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. 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)
. To get the desired string, you also have to add a space, ie re.sub(r'\(+as .*?\) ','',line)
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With