regex puzzle

Mike Brown mike at skew.org
Wed Nov 6 09:56:45 EST 2002


"Alex Martelli" <aleax at aleax.it> wrote in message
news:ET9y9.78516$aL4.2423622 at news1.tin.it...
> Mike Brown wrote:
>
> > I need an example of a regular expression that:
> >
> > - matches an empty string
> > - matches a non-empty string
> > - does NOT match a string consisting of only a linefeed
> >
> > So, this test should produce [1, 1, 0]...
> >
> > import re
> > pat = '^(.+)?$' # FIXME
> > [(re.search(pat, s) is not None) for s in ['', 'foo', '\n']]
>
> import re
> pat = r'\A(?!\n\Z)'
> print [(re.search(pat, s) is not None) for s in
>        ['', 'foo', '\n', '\n\n' ]]
>
> The "this test should produce" request can be satisfied without
> the \Z, but I think you mean that '\n\n' should match (as it
> doesn't consist of only a linefeed, but of two of them), and
> for that you do need the \Z in the negative-lookahead assertion.

Thanks! I think this was close enough to what I wanted that I could figure
out the rest. I didn't want \n\n to match, but that's actually taken care of
by the non-empty string clause, as it is a pattern sort of like (.+)? except
instead of .+ it's a pattern a couple thousand characters long. Making it be
(.+)? for the sake of discussion makes it an easier problem to solve. This
seems to give me what I want: r'\A(?!\n)(.+)?\Z'  ... thanks for getting me
on the right track!

I'm not sure I understand the difference between ^$ and \A\Z, but the (?!\n)
makes sense.







More information about the Python-list mailing list