[Tutor] regex newbie question

Kent Johnson kent37 at tds.net
Thu May 8 20:14:24 CEST 2008


On Thu, May 8, 2008 at 1:51 PM, Dick Moores <rdm at rcblue.com> 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.

Kent


More information about the Tutor mailing list