Re: Trivial string substitution/parser

2007-06-19 Thread Graham Breed
Duncan Booth wote: > If you must insist on using backslash escapes (which introduces the > question of how you get backslashes into the output: do they have to be > escaped as well?) then use string.Template with a custom pattern. If anybody wants this, I worked out the following regular expressi

Re: Trivial string substitution/parser

2007-06-18 Thread Graham Breed
Duncan Booth wote: > Also, of course, vars just needs to be something which quacks like a dict: > it can do whatever it needs to do such as looking up a database or querying > a server to generate the value only when it needs it, or even evaluating > the name as an expression; in the OP's case it

Re: Trivial string substitution/parser

2007-06-18 Thread Duncan Booth
Josiah Carlson <[EMAIL PROTECTED]> wrote: > Samuel wrote: >> On Sun, 17 Jun 2007 11:00:58 +, Duncan Booth wrote: >> >>> The elegant and lazy way would be to change your specification so >>> that $ characters are escaped by $$ not by backslashes. Then you can >>> write: >>> >> from string

Re: Trivial string substitution/parser

2007-06-17 Thread Graham Breed
Samuel wote: > Thanks, however, turns out my specification of the problem was > incomplete: In addition, the variable names are not known at compilation > time. > I just did it that way, this looks fairly easy already: > > --- > import re > > def variable_sub_cb(match): >

Re: Trivial string substitution/parser

2007-06-17 Thread Josiah Carlson
Samuel wrote: > On Sun, 17 Jun 2007 11:00:58 +, Duncan Booth wrote: > >> The elegant and lazy way would be to change your specification so that $ >> characters are escaped by $$ not by backslashes. Then you can write: >> > from string import Template > ... > > Thanks, however, turns o

Re: Trivial string substitution/parser

2007-06-17 Thread Samuel
On Sun, 17 Jun 2007 11:00:58 +, Duncan Booth wrote: > The elegant and lazy way would be to change your specification so that $ > characters are escaped by $$ not by backslashes. Then you can write: > from string import Template ... Thanks, however, turns out my specification of the

Re: Trivial string substitution/parser

2007-06-17 Thread Duncan Booth
Samuel <[EMAIL PROTECTED]> wrote: > Hi, > > How would you implement a simple parser for the following string: > > --- > In this string $variable1 is substituted, while \$variable2 is not. > --- > > I know how to write a parser, but I am looking for an elegant (and lazy) > way. Any idea? > The