<html><head></head><body>Hi,<br>
<br>
\b is defined as all non-word characters, so it is the complement oft \w. \w is [A-Za-z0-9_-], so \b includes \$ and thus cuts off your <sign> group.<br>
<br>
-nik<br><br><div class="gmail_quote"><br>
<br>
Alex Kleider <akleider@sonic.net> schrieb:<blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
<pre class="k9mail">#!/usr/bin/env python<br /><br />"""<br />I've been puzzling over the re module and have a couple of questions<br />regarding the behaviour of this script.<br /><br />I've provided two possible patterns (re_US_money):<br />the one surrounded by the 'word boundary' meta sequence seems not to <br />work<br />while the other one does. I can't understand why the addition of the <br />word<br />boundary defeats the match.<br /><br />I also don't understand why the split method includes the matched text.<br />Splitting only works as I would have expected if no goupings are used.<br /><br />If I've set this up as intended, the full body of this e-mail should be<br />executable as a script.<br /><br />Comments appreciated.<br />alex kleider<br />"""<br /><br /># file : <a href="http://tutor.py">tutor.py</a> (Python 2.7, NOT Python 3)<br />print 'Running "<a href="http://tutor.py">tutor.py</a>" on an Ubuntu Linux machine. *********'<br /><br />import re<br /><br />target = \<br />"""Cost is $4.50. With a $.30 discount:<br />Price is $4.15.<br />The price could be less, say $4 or $4.<br />Let's see how this plays out: $4.50.60<br />"""<br /><br /># Choose one of the following two alternatives:<br />re_US_money =\<br />r"((?P<sign>\$)(?P<dollars>\d{0,})(?:\.(?P<cents>\d{2})){0,1})"<br /># The above provides matches.<br /># The following does NOT.<br /># re_US_money =\<br /># r"\b((?P<sign>\$)(?P<dollars>\d{0,})(?:\.(?P<cents>\d{2})){0,1})\b"<br /><br />pat_object = re.compile(re_US_money)<br />match_object = pat_object.search(target)<br />if match_object:<br />print "'match_object.group()' and 'match_object.span()' yield:"<br />print match_object.group(), match_object.span()<br />print<br />else:<br />print "NO MATCH FOUND!!!"<br />print<br />print "Now will use 'finditer()':"<br /><br />print<br />iterator = pat_object.finditer(target)<br />i = 1<br />for iter in iterator:<br />print<br />print "iter #%d: "%(i, ),<br />print iter.group()<br />print "'groups()' yields: '%s'."%(iter.groups(), )<br />print iter.span()<br />i += 1<br />sign = iter.group("sign")<br />dollars = iter.group("dollars")<br />cents = iter.group("cents")<br />print sign,<br />print " ",<br />if dollars:<br />print dollars,<br />else:<br />print "00",<br />print " ",<br />if cents:<br />print cents,<br />else:<br />print "00",<br /><br />print<br /><br />t = target<br />sub_target = pat_object.sub("<insert value here>", t)<br />print<br />print "Printing substitution: "<br />print sub_target<br />split_target = pat_object.split(target)<br />print "Result of splitting on the target: "<br />print split_target<br /><br /># End of script.<br /><hr /><br />Tutor maillist - Tutor@python.org<br />To unsubscribe or change subscription options:<br /><a href="http://mail.python.org/mailman/listinfo/tutor">http://mail.python.org/mailman/listinfo/tutor</a><br /></pre></blockquote></div><br>
-- <br>
Diese Nachricht wurde von meinem Android-Mobiltelefon mit K-9 Mail gesendet.</body></html>