Re: packing things back to regular expression



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
.



Relevant Pages

  • Re: Simple Regular Expression need
    ... charactors in a string. ... Here you state that everything between the beginning of the string and the end may not contain a,b or c. ... If you want to validate dates stricter I think there are some javescript functions you can rely on. ... I am not familiar with Regular Expression and I am on a tight project ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: fixing up a string to make it a valid XML attribute name ...
    ... trying to validate a string to see if it can be a valid XML ... I'm guessing writing a regular expression is the way forward. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Help with a regular expression
    ... Can someone help me with a regular expression to validate the ... following string: ... wscript.echo .test(str) & vbTab & str ...
    (microsoft.public.scripting.vbscript)
  • Re: Regexp only if string is a certain length
    ... >I need to validate a text box entry, but ONLY if it is 17 characters, ... My regular expression for the ... any string up to 16 chars in length ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: Get regular expression
    ... own tree structure. ... Expression compares a string character-by character, ... regular expression solution, which was about as close as one could get to ... the structure of the hierarchy can be inferred by using ...
    (microsoft.public.dotnet.languages.csharp)

Loading