packing things back to regular expression

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Wed Feb 20 19:29:32 EST 2008


On Wed, 20 Feb 2008 11:36:20 -0800, Amit Gupta wrote:

> Before I read the message: I screwed up.
> 
> Let me write again
> 
>>> x = re.compile("CL(?P<name1>[a-z]+)")
> # group name "name1" is attached to the match of lowercase string of
> alphabet
> # Now I have a dictionary saying {"name1", "iamgood"} 
> # I would like a function, that takes x and my dictionary and 
> return "CLiamgood"
> # If my dictionary instead have {"name1", "123"}, it gives error on
> processingit
> #
> # In general, I have reg-expression where every non-trivial match has a
> group-name. I want to do the reverse of reg-exp match. The function can
> take reg-exp and replace the group-matches from dictionary
> # I hope, this make it clear.


Clear as mud. But I'm going to take a guess.

Are you trying to validate the data against the regular expression as 
well as substitute values? That means your function needs to do something 
like this:

(1) Take the regular expression object, and extract the string it was 
made from. That way at least you know the regular expression was valid.

x = re.compile("CL(?P<name1>[a-z]+)")  # validate the regex
x.pattern()

=> "CL(?P<name1>[a-z]+)"


(2) Split the string into sets of three pieces:

split("CL(?P<name1>[a-z]+)")  # you need to write this function

=> ("CL", "(?P<name1>", "[a-z]+)")


(3) Mangle the first two pieces:

mangle("CL", "(?P<name1>")  # you need to write this function

=> "CL%(name1)s"

(4) Validate the value in the dictionary:

d = {"name1", "123"}
validate("[a-z]+)", d)

=> raise exception

d = {"name1", "iamgood"} 
validate("[a-z]+)", d)

=> return True


(5) If the validation step succeeded, then do the replacement:

"CL%(name1)s" % d

=> "CLiamgood"


Step (2), the splitter, will be the hardest because you essentially need 
to parse the regular expression. You will need to decide how to handle 
regexes with multiple "bits", including *nested* expressions, e.g.:

"CL(?P<name1>[a-z]+)XY(?:AB)[aeiou]+(?P<name2>CD(?P<name3>..)\?EF)"


Good luck.


-- 
Steven



More information about the Python-list mailing list