[Tutor] RE Silliness
Omer
Jaggojaggo+Py at gmail.com
Mon Jan 5 17:16:13 CET 2009
Bob, I tried your way.
>>> import re
>>> urlMask = r"http://[\w\Q./\?=\R]+(<br>)?"
>>> text=u"Not working example<br>http://this.is.a/url?header=null<br>And
another line<br>http://and.another.url"
>>> re.findall(urlMask,text)
[u'<br>', u'']
spir, I did understand it. What I'm not understanding is why isn't this
working.
(Whereas,
>>> OldurlMask = r"http://[\w\Q./\?=\R]+" #Not f-ing working.
>>> re.findall(OldurlMask,text)
['http://this.is.a/url?header=null', 'http://and.another.url']
does work. Which is what had me frowning.
Also,
this ugly url mask is working:
>>> UglyUrlMask = r"(http://[\w\Q./\?=\R]+<br>|http://[\w\Q./\?=\R]+)"
>>> re.findall(UglyUrlMask,text)
['http://this.is.a/url?header=null<br>', 'http://and.another.url']
Anyone?)
On Mon, Jan 5, 2009 at 12:08 AM, spir <denis.spir at free.fr> wrote:
> On Sun, 04 Jan 2009 14:09:53 -0500
> bob gailer <bgailer at gmail.com> wrote:
>
> > Omer wrote:
> > > I'm sorry, burrowed into the reference until my eyes bled.
> > >
> > > What I want is to have a regular expression with an optional ending of
> > > "<br>"
> > >
> > > (For those interested,
> > > urlMask = r"http://[\w\Q./\?=\R]+"
> > > is ther version w/o the optional <br> ending.)
> > >
> > > I can't seem to make a string optional- only a single character via
> > > []s. I for some reason thuoght it'll be ()s, but no help there- it
> > > just returns only the <br>. Anybody?
> > >
> > urlMask = r"http://[\w\Q./\?=\R]+(<br>)?"
> >
> > From the docs: ? Causes the resulting RE to match 0 or 1 repetitions of
> > the preceding RE. ab? will match either 'a' or 'ab'.
> >
> >
>
> Maybe Omer had not noted that a sub-expression can be grouped in () so that
> an operator (?+*) applies on the whole group.
> Denis
>
> ------
> la vida e estranya
> _______________________________________________
> Tutor maillist - Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20090105/661cb083/attachment.htm>
More information about the Tutor
mailing list