Replacing : with "${" at the beginning of the word and adding "}" at the end of the word

Chris Angelico rosuav at gmail.com
Tue Oct 2 08:41:53 EDT 2018


On Tue, Oct 2, 2018 at 10:36 PM <zljubisic at gmail.com> wrote:
>
> Hi,
>
> if I have a string:
>
> sql = """
> where 1 = 1
> and field = :value
> and field2 in (:list)
> """
>
> I would like to replace every word that starts with ":" in the following way:
> 1. replace ":" with "${"
> 2. at the end of the word add "}"
>
>
> An example should look like this:
>
> where 1 = 1
> and field = ${value}
> and field2 in (${list})
>
> How to do that?

First off: WHY? Are you then planning to do a naive interpolation?

Are you trying to recognize syntactic elements in SQL? If so, you'll
need an SQL parser. Or are you prepared to handle just a few basics,
like making sure the colon isn't inside quotes? And if so, which
basics? And what constitutes a word?

I strongly recommend *not doing this* unless you have an extremely
good reason to.

ChrisA



More information about the Python-list mailing list