regular expression

P

Pierre

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 ?

Thanks.
 
S

Steven D'Aprano

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 = s.replace(c, 'x[%s]'%c)
....'(x[1] & (x[2] || x[3]))'
 
P

Peter Otten

Pierre said:
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 ?
re.compile(r"(\d+)").sub(r"x[\1]", "(1 and (2 or 3))") '(x[1] and (x[2] or x[3]))'
re.compile("and|or").sub(lambda m, d={"and":"&", "or":"||"}:
d[m.group()], _)
'(x[1] & (x[2] || x[3]))'

Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,821
Latest member
AleidaSchi

Latest Threads

Top