Pattern-match & Replace - help required

Discussion in 'Python' started by AT, Dec 19, 2012.

  1. AT

    AT Guest

    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
     
    AT, Dec 19, 2012
    #1
    1. Advertising

  2. 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
     
    Steven D'Aprano, Dec 19, 2012
    #2
    1. Advertising

  3. AT

    AT Guest

    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
     
    AT, Dec 19, 2012
    #3
  4. 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[:eek:ffset] + 'spam' + lunch[offset + len('ham'):]
    print(lunch)




    --
    Steven
     
    Steven D'Aprano, Dec 19, 2012
    #4
  5. AT

    Thomas Bach Guest

    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.
     
    Thomas Bach, Dec 19, 2012
    #5
  6. AT

    AT Guest

    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
     
    AT, Dec 19, 2012
    #6
  7. AT

    AT Guest

    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
     
    AT, Dec 19, 2012
    #7
  8. AT

    Peter Otten Guest

    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)
     
    Peter Otten, Dec 19, 2012
    #8
  9. AT

    AT Guest

    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
     
    AT, Dec 19, 2012
    #9
  10. AT

    AT Guest

    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
     
    AT, Dec 19, 2012
    #10
  11. Alexander Blinne, Dec 19, 2012
    #11
  12. AT

    MRAB Guest

    MRAB, Dec 19, 2012
    #12
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. RV
    Replies:
    1
    Views:
    452
    Donald 'Paddy' McCarthy
    Oct 23, 2003
  2. John Gordon
    Replies:
    13
    Views:
    498
    Ian Kelly
    Dec 20, 2011
  3. Prasad S
    Replies:
    2
    Views:
    245
    Dr John Stockton
    Aug 27, 2004
  4. RV
    Replies:
    6
    Views:
    141
    Darren Dunham
    Oct 23, 2003
  5. Replies:
    3
    Views:
    177
    Brian McCauley
    Sep 12, 2005
Loading...

Share This Page