packing things back to regular expression

Gary Herron gherron at islandtraining.com
Wed Feb 20 13:50:21 EST 2008


Amit Gupta wrote:
> Hi
>
> I wonder if python has a function to pack things back into regexp,
> that has group names.
>
> e.g:
> exp = (<?P<name1>[a-z]+)
> compiledexp = re.compile(exp)
>
> Now, I have a dictionary "mytable = {"a" : "myname"}
>
> Is there a way in re module, or elsewhere, where I can have it match
> the contents from dictionary to the re-expression (and check that it
> matches the rules) and than return the substituted string?
>   
I'm not following what you're asking for until I get to the last two 
words.  The re module does have functions to do string substitution.  
One or more occurrences  of a pattern matched by an re can be replaces 
with a given string.  See sub and subn.  Perhaps you can make one of 
those do whatever it is you are trying to do.

Gary Herron

> e.g
>   
>>> re.SomeNewFunc(compilexp, mytable)
>>>       
> "myname"
>   
>>> mytable = {"a" : "1"}
>>> re.SomeNewFunc(compileexp, mytable)
>>>       
> ERROR
>
>
>
> Thanks
> A
>   




More information about the Python-list mailing list