Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Pattern-match & Replace - help required

Reply
Thread Tools

Pattern-match & Replace - help required

 
 
AT
Guest
Posts: n/a
 
      12-19-2012
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


 
Reply With Quote
 
 
 
 
Steven D'Aprano
Guest
Posts: n/a
 
      12-19-2012
On Wed, 19 Dec 2012 02:42:26 -0800, AT wrote:

> 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.



--
Steven
 
Reply With Quote
 
 
 
 
AT
Guest
Posts: n/a
 
      12-19-2012

On Wednesday, 19 December 2012 15:51:22 UTC+5, Steven D'Aprano wrote:
> On Wed, 19 Dec 2012 02:42:26 -0800, AT wrote:
>
>
>
> > 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.
>
>
>
>
>
>
>
> --
>
> Steven



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

hope it clarifies?

thanks


 
Reply With Quote
 
Steven D'Aprano
Guest
Posts: n/a
 
      12-19-2012
On Wed, 19 Dec 2012 03:01:32 -0800, AT wrote:

> 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[ffset] + 'spam' + lunch[offset + len('ham'):]
print(lunch)




--
Steven
 
Reply With Quote
 
Thomas Bach
Guest
Posts: n/a
 
      12-19-2012
On Wed, Dec 19, 2012 at 02:42:26AM -0800, AT wrote:
> 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, ham, tomato'.replace('ham', 'spam, ham, spam')

'egg, spam, ham, spam, tomato'


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

Regards,
Thomas.
 
Reply With Quote
 
AT
Guest
Posts: n/a
 
      12-19-2012
On Wednesday, 19 December 2012 16:27:19 UTC+5, Thomas Bach wrote:
> On Wed, Dec 19, 2012 at 02:42:26AM -0800, AT wrote:
>
> > 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, ham, tomato'.replace('ham', 'spam, ham, spam')

>
> '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
 
Reply With Quote
 
AT
Guest
Posts: n/a
 
      12-19-2012
On Wednesday, 19 December 2012 16:27:19 UTC+5, Thomas Bach wrote:
> On Wed, Dec 19, 2012 at 02:42:26AM -0800, AT wrote:
>
> > 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, ham, tomato'.replace('ham', 'spam, ham, spam')

>
> '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
 
Reply With Quote
 
Peter Otten
Guest
Posts: n/a
 
      12-19-2012
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,
> 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)

 
Reply With Quote
 
AT
Guest
Posts: n/a
 
      12-19-2012
On Wednesday, 19 December 2012 18:16:18 UTC+5, Peter Otten wrote:
> 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,

>
> > 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)


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

Regards
 
Reply With Quote
 
AT
Guest
Posts: n/a
 
      12-19-2012
On Wednesday, 19 December 2012 18:16:18 UTC+5, Peter Otten wrote:
> 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,

>
> > 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)


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

Regards
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Replace /n with a XHTML <br /> using string.replace Alun ASP .Net 3 02-18-2008 05:52 AM
Re: [Pyrex] pyrex functions to replace a method (Re: replace a method Greg Ewing Python 2 06-29-2006 05:25 PM
pyrex functions to replace a method (Re: replace a method in class:how?) Brian Blais Python 1 06-27-2006 12:13 PM
s/$match/$replace/ fails when $replace has backreferences anonyusenet@yahoo.com Perl Misc 3 09-12-2005 11:03 AM
help with string replace - for doing selective replace Prasad S Javascript 2 08-27-2004 03:22 PM



Advertisments