regular expression

Steven D'Aprano steven at REMOVE.THIS.cybersource.com.au
Thu Aug 20 09:40:06 CEST 2009


On Thu, 20 Aug 2009 00:18:23 -0700, Pierre wrote:

> Hello,
> 
> I would like to change the string "(1 and (2 or 3))"  by  "(x[1] & (x
> [2] || x[3]))" using regular expression... Anyone can help me ?


Do you mean you want to change the string into "(x[1] & (x[2] || x[3]))" ?

Does it have to be using regular expressions? Would this be good enough?


>>> s = "(1 and (2 or 3))"
>>> for c in '123':
...     s = s.replace(c, 'x[%s]'%c)
...
>>> s = s.replace('or', '||')
>>> s = s.replace('and', '&')
>>> s
'(x[1] & (x[2] || x[3]))'



-- 
Steven



More information about the Python-list mailing list