[Tutor] regexp
Steven D'Aprano
steve at pearwood.info
Sat Nov 5 00:29:40 CET 2011
Dinara Vakhitova wrote:
> Thank you for your answer, Steven.
>
> Of course it would have been easier to write this function,
> but unfortunately my task is to do it with a regular expression :(
Is this homework? You should have said so.
I don't understand questions like this. Do carpenters ask their
apprentices to cut a piece of wood with a hammer? Do apprentice chefs
get told to dice carrots using only a spoon? Computer programming is the
only skill I know of where teachers routinely insist that students use
inappropriate tools to solve a problem, just to prove they can do it.
In any case, if this is even possible using regular expressions -- and I
don't think it is -- I have no idea how to do it. Good luck. Maybe
somebody else might have a clue.
I don't think it's possible because you don't know how many characters
the string will have. Even if [\1-z] works (which it doesn't), you still
have the same problem that you don't know where to stop:
[a-z][\1-z][\2-z][\3-z][\4-z]...[\99-z]
This is related to the reason you can't parse indefinitely nested
parentheses using a regular expression.
--
Steven
More information about the Tutor
mailing list