[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 <rdm at rcblue.com> wrote:<br>
> ><br>
> > Could someone tell me what's wrong with this regex?<br>
> ><br>
> > ==============================================<br>
> > lst = ["2/2/2", "3/3/45",
> "345/03/45", "4/4/2009", "4/4/12345",<br>
> >
> "12/12/555", "12/12", "2/2",
> "2/12", "12/2"]<br>
> ><br>
> > regex =
> r"\b\d+/\d+/\d{2,4}\b|\b\d{1,2}/\d{1,2}\b"<br><br>
> \b matches the boundary between word and non-word. \ is a non-word<br>
> character so each number is a word and "2/2/2" 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"^\d+/\d+/\d{2,4}$|^\d{1,2}/\d{1,2}$"<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