0

I have this regular expression to validate that a string is a valid url:

/((([A-Za-z]{3,9}:(?:\/\/)?)(?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.-]+(:[0-9]+)?|(?:www.|[-;:&=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-]*)?\??(?:[-\+=&;%@.\w]*)#?(?:[\w]*))?)/

I do not know how to use it.

url="www.google.com"
print(re.compile(r'/((([A-Za-z]{3,9}:(?:\/\/)?)(?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.-]+(:[0-9]+)?|(?:www.|[-;:&=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-]*)?\??(?:[-\+=&;%@.\w]*)#?(?:[\w]*))?)/',url))

Can someone give me a hand please? thank you

yavg
  • 2,761
  • 7
  • 45
  • 115
  • Do to use random regular expressions to validate URLs. Almost all of them are flawed. Use [specialized tools](https://stackoverflow.com/questions/7160737/python-how-to-validate-a-url-in-python-malformed-or-not). – DYZ Mar 17 '19 at 05:39
  • Also see [THIS](https://stackoverflow.com/a/835527/4492932). – DYZ Mar 17 '19 at 05:41

1 Answers1

2

For starters you don't need the first and last slash python automatically uses them, secondly you need to make your expression a re object

import re
expr = re.compile(r'((([A-Za-z]{3,9}:(?:\/\/)?)(?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.-]+(:[0-9]+)?|(?:www.|[-;:&=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-]*)?\??(?:[-\+=&;%@.\w]*)#?(?:[\w]*))?)')

after that you can use the function match to see if the expression returns a valid instance

url="www.google.com"

if expr.match(url):
    print("It is valid")
Mntfr
  • 483
  • 6
  • 20