Pattern-match & Replace - help required

A

AT

Hi,

I am new to python and web2py framework. Need urgent help to match a pattern in an string and replace the matched text.

I've this string (basically an sql statement):
stmnt = 'SELECT taxpayer.id,
taxpayer.enc_name,
taxpayer.age,
taxpayer.occupation
FROM taxpayer WHERE (taxpayer.id IS NOT NULL);'

The requirement is to replace it with this one:
r_stmnt = 'SELECT taxpayer.id,
decrypt(taxpayer.enc_name),
taxpayer.age,
taxpayer.occupation
FROM taxpayer WHERE (taxpayer.id IS NOT NULL);'

Can somebody please help?

Thanks & Regards
 
S

Steven D'Aprano

Hi,

I am new to python and web2py framework. Need urgent help to match a
pattern in an string and replace the matched text.

I've this string (basically an sql statement):

stmnt = 'SELECT taxpayer.id,
taxpayer.enc_name,
taxpayer.age,
taxpayer.occupation
FROM taxpayer WHERE (taxpayer.id IS NOT NULL);'

The requirement is to replace it with this one:

r_stmnt = 'SELECT taxpayer.id,
decrypt(taxpayer.enc_name),
taxpayer.age,
taxpayer.occupation
FROM taxpayer WHERE (taxpayer.id IS NOT NULL);'

Can somebody please help?

Can you do this?

stmnt = r_stmnt

That should do what you are asking.

If that doesn't solve your problem, you will need to explain your problem
in more detail.
 
A

AT

Can you do this?



stmnt = r_stmnt



That should do what you are asking.



If that doesn't solve your problem, you will need to explain your problem

in more detail.


I just wanted to change taxpayer.enc_name in stmnt to decrypt(taxpayer.enc_name)

hope it clarifies?

thanks
 
S

Steven D'Aprano

I just wanted to change taxpayer.enc_name in stmnt to
decrypt(taxpayer.enc_name)

hope it clarifies?

Maybe. Does this help?

lunch = "Bread, ham, cheese and tomato."
# replace ham with spam
offset = lunch.find('ham')
if offset != -1:
lunch = lunch[:eek:ffset] + 'spam' + lunch[offset + len('ham'):]
print(lunch)
 
T

Thomas Bach

Hi,

I am new to python and web2py framework. Need urgent help to match a
pattern in an string and replace the matched text.

Well, what about str.replace then?
'egg, spam, ham, spam, tomato'


If the pattern you want to match is more complicated, have a look at
the re module!

Regards,
Thomas.
 
A

AT

Well, what about str.replace then?




'egg, spam, ham, spam, tomato'





If the pattern you want to match is more complicated, have a look at

the re module!



Regards,

Thomas.


The pattern is '%s.enc_%s', and after matching this pattern want to change it to 'decrypt(%s.enc_%s)'

Thanks
 
A

AT

Well, what about str.replace then?




'egg, spam, ham, spam, tomato'





If the pattern you want to match is more complicated, have a look at

the re module!



Regards,

Thomas.


The pattern is '%s.enc_%s', and after matching this pattern want to change it to 'decrypt(%s.enc_%s)'

Thanks
 
P

Peter Otten

AT said:
I am new to python and web2py framework. Need urgent help to match a
pattern in an string and replace the matched text.

I've this string (basically an sql statement):
stmnt = 'SELECT taxpayer.id,
taxpayer.enc_name,
taxpayer.age,
taxpayer.occupation
FROM taxpayer WHERE (taxpayer.id IS NOT NULL);'

The requirement is to replace it with this one:
r_stmnt = 'SELECT taxpayer.id,
decrypt(taxpayer.enc_name),
taxpayer.age,
taxpayer.occupation
FROM taxpayer WHERE (taxpayer.id IS NOT NULL);'

Can somebody please help?
The pattern is '%s.enc_%s', and after matching this pattern want to change
it to 'decrypt(%s.enc_%s)'

after = re.compile(r"(\w+[.]enc_\w+)").sub(r"decrypt(\1)", before)
 
A

AT

AT wrote:


I am new to python and web2py framework. Need urgent help to match a
pattern in an string and replace the matched text.

I've this string (basically an sql statement):
stmnt = 'SELECT taxpayer.id,



FROM taxpayer WHERE (taxpayer.id IS NOT NULL);'

The requirement is to replace it with this one:
r_stmnt = 'SELECT taxpayer.id,



FROM taxpayer WHERE (taxpayer.id IS NOT NULL);'

Can somebody please help?


The pattern is '%s.enc_%s', and after matching this pattern want to change
it to 'decrypt(%s.enc_%s)'



after = re.compile(r"(\w+[.]enc_\w+)").sub(r"decrypt(\1)", before)

Thanks a million
Can you recommend a good online book/tutorial on regular expr. in python?

Regards
 
A

AT

AT wrote:


I am new to python and web2py framework. Need urgent help to match a
pattern in an string and replace the matched text.

I've this string (basically an sql statement):
stmnt = 'SELECT taxpayer.id,



FROM taxpayer WHERE (taxpayer.id IS NOT NULL);'

The requirement is to replace it with this one:
r_stmnt = 'SELECT taxpayer.id,



FROM taxpayer WHERE (taxpayer.id IS NOT NULL);'

Can somebody please help?


The pattern is '%s.enc_%s', and after matching this pattern want to change
it to 'decrypt(%s.enc_%s)'



after = re.compile(r"(\w+[.]enc_\w+)").sub(r"decrypt(\1)", before)

Thanks a million
Can you recommend a good online book/tutorial on regular expr. in python?

Regards
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top