Simple regex with whitespaces

John Machin sjmachin at lexicon.net
Mon Sep 11 18:48:26 EDT 2006


James Stroud wrote:
> Paul McGuire wrote:
> > <mathieu.malaterre at gmail.com> wrote in message
> > news:1157945804.805624.32620 at p79g2000cwp.googlegroups.com...
> >
> >>Hello,
> >>
> >> I cannot figure out a way to find a regular expression that would
> >>match one and only one of these two strings:
> [clip]
> >>Any suggestion ? Thanks a bunch !
> >>Mathieu
> >>
> >
> > A pyparsing approach is not as terse as regexp's, but it's not terribly long
> > either.
>
> To second Paul's suggestion, usually, if the regex is not immediately
> obvious, its probably better to look into parsing modules, pyparsing
> being one of the most accessible modules (to me, anyways)--and well
> worth learning.

I would say that it's better, before leaping to the implementation, to
understand the problem. IOW, rough out the grammar first --- in the
current case, not very complicated at all; the way I looked at it there
are only two possible outcomes from the tokeniser (space and not-space)
and only three non-terminal symbols (word, sentence, paragraph) -- then
choose the implementation.

>
> However, in complicated applications, regex is usually still fun and
> valuable as an intellectual exercise.

Indeed. Better use of the mind and the CPU than distractions du jour
like pseudorubiku or whatever it's called :-)

Cheers,
John




More information about the Python-list mailing list