Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a single function for matching and replacing?

Tags:

python

regex

I wonder if there is a simpler alternative (e.g. a single function call) for matching and replacing to the following example:

>>> import re
>>> 
>>> line = 'file:///windows-d/academic%20discipline/study%20objects/areas/formal%20systems/math'
>>> 
>>> match = re.match(r'^file://(.*)$', line)
>>> if match and match.group(1):
...     substitution = re.sub(r'%20', r' ', match.group(1))
... 
>>> substitution
'/windows-d/academic discipline/study objects/areas/formal systems/math'

Thanks.

like image 848
Tim Avatar asked Mar 14 '26 09:03

Tim


1 Answers

I'm going to dodge your regex question and suggest you use something else for this:

>>> line = 'file:///windows-d/academic%20discipline/study%20objects/areas/formal%20systems/math' 
>>> import urllib
>>> urllib.unquote(line)
'file:///windows-d/academic discipline/study objects/areas/formal systems/math'

Then just strip off the file:// with a slice or str.replace if necessary.

%20 (space) is not the only escaped character possible here, so it's better to use the right tool for the job than have your regex solution break later when there is another character needing un-escaping.

like image 161
wim Avatar answered Mar 15 '26 22:03

wim



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!