July 19th, 2005, 07:43 AM
regular expression like sed
Can python alllow you to write expression like sed does ?
test = s/(this)is(who)/\2 is \1/
July 19th, 2005, 08:19 AM
What is the result?
Why would anyone like to write something that cryptic?
July 19th, 2005, 02:34 PM
Yes it can. There is a regex module called re, so it is not quite as convenient as languages that have regex built into the core language, but IMHO is more powerful.
Originally Posted by oppai
The python equivalent is
Dave - The Developers' Coach
>>> import re
>>> s = 'this is who'
>>> re.sub( r'(this) is (who)', r'\2 is \1', s)
'who is this'
July 20th, 2005, 07:39 AM
now it makes sense. I am learning.
July 20th, 2005, 11:39 PM
with python, you also don't have to use re's they are very cryptic and hard to read. I personly do not like them. You can write something like this instead in python that is built into the language.
>>> s = "this is who"
>>> sp = s.split()
>>> end = sp + " " + sp + " " + sp
>>> print end
who is this
now that is a lot easyer to read and it does the same exact thing. I know there are better ways to do this, sorry it is like 3 in the morning and that was the first way that came to my head. I hope I got the re translation right, I havn't played with them in ever.