Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #35090 > unrolled thread

Pattern-match & Replace - help required

Started byAT <matifayaz@gmail.com>
First post2012-12-19 02:42 -0800
Last post2012-12-19 05:41 -0800
Articles 12 — 6 participants

Back to article view | Back to comp.lang.python


Contents

  Pattern-match & Replace - help required AT <matifayaz@gmail.com> - 2012-12-19 02:42 -0800
    Re: Pattern-match & Replace - help required Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-12-19 10:51 +0000
      Re: Pattern-match & Replace - help required AT <matifayaz@gmail.com> - 2012-12-19 03:01 -0800
        Re: Pattern-match & Replace - help required Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-12-19 11:09 +0000
    Re: Pattern-match & Replace - help required Thomas Bach <thbach@students.uni-mainz.de> - 2012-12-19 12:27 +0100
      Re: Pattern-match & Replace - help required AT <matifayaz@gmail.com> - 2012-12-19 03:53 -0800
      Re: Pattern-match & Replace - help required AT <matifayaz@gmail.com> - 2012-12-19 03:53 -0800
    Re: Pattern-match & Replace - help required Peter Otten <__peter__@web.de> - 2012-12-19 14:16 +0100
      Re: Pattern-match & Replace - help required AT <matifayaz@gmail.com> - 2012-12-19 05:41 -0800
        Re: Pattern-match & Replace - help required Alexander Blinne <news@blinne.net> - 2012-12-19 15:11 +0100
          Re: Pattern-match & Replace - help required MRAB <python@mrabarnett.plus.com> - 2012-12-19 14:23 +0000
      Re: Pattern-match & Replace - help required AT <matifayaz@gmail.com> - 2012-12-19 05:41 -0800

#35090 — Pattern-match & Replace - help required

FromAT <matifayaz@gmail.com>
Date2012-12-19 02:42 -0800
SubjectPattern-match & Replace - help required
Message-ID<e6346fb0-59a2-4e76-9bbf-6f04521e41cd@googlegroups.com>
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
 

[toc] | [next] | [standalone]


#35092

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2012-12-19 10:51 +0000
Message-ID<50d19c29$0$29991$c3e8da3$5496439d@news.astraweb.com>
In reply to#35090
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

[toc] | [prev] | [next] | [standalone]


#35094

FromAT <matifayaz@gmail.com>
Date2012-12-19 03:01 -0800
Message-ID<6ff2d112-aff7-4f32-956c-2136ada81f6e@googlegroups.com>
In reply to#35092
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

[toc] | [prev] | [next] | [standalone]


#35096

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2012-12-19 11:09 +0000
Message-ID<50d1a07b$0$29991$c3e8da3$5496439d@news.astraweb.com>
In reply to#35094
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[:offset] + 'spam' + lunch[offset + len('ham'):]
print(lunch)




-- 
Steven

[toc] | [prev] | [next] | [standalone]


#35098

FromThomas Bach <thbach@students.uni-mainz.de>
Date2012-12-19 12:27 +0100
Message-ID<mailman.1041.1355916450.29569.python-list@python.org>
In reply to#35090
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.

[toc] | [prev] | [next] | [standalone]


#35103

FromAT <matifayaz@gmail.com>
Date2012-12-19 03:53 -0800
Message-ID<e9d5c9c4-0dc1-4ac6-90b5-8252df201aa8@googlegroups.com>
In reply to#35098
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

[toc] | [prev] | [next] | [standalone]


#35104

FromAT <matifayaz@gmail.com>
Date2012-12-19 03:53 -0800
Message-ID<mailman.1042.1355917989.29569.python-list@python.org>
In reply to#35098
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

[toc] | [prev] | [next] | [standalone]


#35108

FromPeter Otten <__peter__@web.de>
Date2012-12-19 14:16 +0100
Message-ID<mailman.1046.1355922991.29569.python-list@python.org>
In reply to#35090
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)

[toc] | [prev] | [next] | [standalone]


#35110

FromAT <matifayaz@gmail.com>
Date2012-12-19 05:41 -0800
Message-ID<d922c9d6-1707-42c9-9f9a-faed4ee57d88@googlegroups.com>
In reply to#35108
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

[toc] | [prev] | [next] | [standalone]


#35112

FromAlexander Blinne <news@blinne.net>
Date2012-12-19 15:11 +0100
Message-ID<50d1cafc$0$6563$9b4e6d93@newsspool4.arcor-online.net>
In reply to#35110
Am 19.12.2012 14:41, schrieb AT:
> Thanks a million
> Can you recommend a good online book/tutorial on regular expr. in python?

http://docs.python.org/3/howto/regex.html

[toc] | [prev] | [next] | [standalone]


#35117

FromMRAB <python@mrabarnett.plus.com>
Date2012-12-19 14:23 +0000
Message-ID<mailman.1048.1355927033.29569.python-list@python.org>
In reply to#35112
On 2012-12-19 14:11, Alexander Blinne wrote:
> Am 19.12.2012 14:41, schrieb AT:
>> Thanks a million
>> Can you recommend a good online book/tutorial on regular expr. in python?
>
> http://docs.python.org/3/howto/regex.html
>
Another good resource is:

http://www.regular-expressions.info/

[toc] | [prev] | [next] | [standalone]


#35137

FromAT <matifayaz@gmail.com>
Date2012-12-19 05:41 -0800
Message-ID<mailman.1060.1355935196.29569.python-list@python.org>
In reply to#35108
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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web