[Tutor] regex newbie question

Steve Willoughby steve at alchemy.com
Thu May 8 20:46:59 CEST 2008


Be aware that \d{2,4} matches 2, 3 or 4 digits, which may be
different than what you're looking for, since 1/12/234 would
match

On Thu, May 8, 2008 11:42, Dick Moores wrote:
> <html>
> <body>
> At 11:14 AM 5/8/2008, Kent Johnson wrote:<br>
> <blockquote type=cite class=cite cite="">On Thu, May 8, 2008 at 1:51 PM,
> Dick Moores &lt;rdm at rcblue.com&gt; wrote:<br>
> &gt;<br>
> &gt;&nbsp; Could someone tell me what's wrong with this regex?<br>
> &gt;<br>
> &gt;&nbsp; ==============================================<br>
> &gt;&nbsp; lst = [&quot;2/2/2&quot;, &quot;3/3/45&quot;,
> &quot;345/03/45&quot;, &quot;4/4/2009&quot;, &quot;4/4/12345&quot;,<br>
> &gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
> &quot;12/12/555&quot;, &quot;12/12&quot;, &quot;2/2&quot;,
> &quot;2/12&quot;, &quot;12/2&quot;]<br>
> &gt;<br>
> &gt;&nbsp; regex =
> r&quot;\b\d+/\d+/\d{2,4}\b|\b\d{1,2}/\d{1,2}\b&quot;<br><br>
> \b matches the boundary between word and non-word. \ is a non-word<br>
> character so each number is a word and &quot;2/2/2&quot; will match
> against<br>
> \b\d{1,2}/\d{1,2}\b. The regex only has to match some part of the<br>
> string, not the whole string.<br><br>
> If you want to match against the full string then use ^ and $ at<br>
> beginning and end of the regex rather than \b.</blockquote><br>
> Changing to <br>
> <tt>regex = r&quot;^\d+/\d+/\d{2,4}$|^\d{1,2}/\d{1,2}$&quot;<br><br>
> </tt>did the job, and I learned several important points.<br><br>
> Thanks, Kent and Steve!<br><br>
> Dick<br>
> </body>
> </html>
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>




More information about the Tutor mailing list