regex puzzle
Mike Brown
mike at skew.org
Wed Nov 6 10:25:23 EST 2002
Harvey Thomas wrote:
> 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']]
> >
> > Any suggestions? :)
> >
>
> Why do you need a regex, or is there something you've missed out. As stated above, how about:
>
> >>> [s != '\n' for s in ['', 'foo', '\n']]
> [1, 1, 0]
Heh. Well, when I said "matches a non-empty string", I meant "matches certain
non-empty strings". More accurately, I need to match either the empty string
or a very lengthy pattern (2.8K, for strict validation of URI references). To
keep the test case simple, I simplified this to "(.+)?", but didn't make it so
clear when I asked the question on comp.lang.python.
I believe I got it working now with r"\A(?!\n)" + MYPATTERN + r"\Z" after
someone replying in the newsgroup got me on the right track. The fruit of this
labor is in
http://cvs.4suite.org/cgi-bin/viewcvs.cgi/4Suite/Ft/Lib/Uri.py?rev=1.32&content-type=text/vnd.viewcvs-markup
down around STRICT_URIREF_PYREGEX, providing the functionality for a URI
reference validator, matchesUriRefSyntax().
- Mike
____________________________________________________________________________
mike j. brown | xml/xslt: http://skew.org/xml/
denver/boulder, colorado, usa | resume: http://skew.org/~mike/resume/
More information about the Python-list
mailing list