[Tutor] Regexes -- \b ; re.sub
John Abbe
johnca@ourpla.net
Mon Nov 18 05:31:35 2002
At 7:28 PM +0100 on 2002-11-16, Magnus Lycka typed:
>At 15:09 2002-11-16 +0630, John Abbe wrote:
>>matches = re.search (re.compile ("(.)\b(.)"), "spam and eggs are yummy")
>
>I've never seen anyone send a regular expression object to re.search
>like that before.
Nice to know i can be original :). I'm learning mostly from the
O'Reilly pocket reference, so i'm making a lot of things up as i go
along. I was wondering why it was so cumbersome.
>You can either write:
>
>pattern = re.compile(r"(.)\b(.)")
>matches = pattern.search("spam and eggs are yummy")
>
>or shorter
>
>matches = re.compile(r"(.)\b(.)").search("spam and eggs are yummy")
>
>or use the re.search function with an (uncompiled) pattern.
>
>matches = re.search(r"(.)\b(.)", "spam and eggs are yummy")
...
>It's a good thing
>to follow the official Python style guide:
>http://www.python.org/peps/pep-0008.html
Thanks for the pointers.
>I'm not sure why you called your match object "matches". If you want
>to find all non-overlapping <something><word boundry><something> you
>could use re.findall(pattern, text) or re.compile(pattern).findall(text)
Since i had more than one parentheses, i was thinking of it as
multiple matches. I guess the Python lingo for that would be "groups"
and "matches" would be what you're describing here.
For any interested, i've implemented a simple wiki parser for
pyblosxom, and am working on TwinPages (aka SisterSites) for
PikiePikie. Life,
John
--
All you /\/\ John Abbe "If you don't like the news,
need \ / CatHerder go out and make some of your own."
is... \/ http://www.ourpla.net/john/ --Wes Nisker