How do I rewrite this new way to recognise addresses to work in Python?
\b(([\w-]+://?|www[.])[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|/)))
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The original source for that states “This pattern should work in most modern regex implementations” and specifically Perl. Python’s regex implementation is modern and similar to Perl’s but is missing the
[:punct:]character class. You can easily build that using this:The
re.sub()call escapes certain characters inside the character set as required.Edit: Using re.escape() works just as well, since it just sticks a backslash in front of everything. That felt crude to me at first, but certainly works fine for this case.