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