How can I use if else for regex in flask

Issue

In my Flask app, users can type a url for their post. But I want let them to use only a-zA-Z0-9_- in the url. If anything else includes in the url more than the a-zA-Z0-9_- then I want to return "False" and otherwise "True"
Here is a some demonstrate.

url = request.form.get('user_url')

if url not in a-zA-Z0-9_- structure :
   return False
else:
   return True

How can I use python regex to make this happens ?

Solution

Ok, I could make it with @Tim Biegeleisen’s answer. Here is the answer.

if re.search(r'[^a-zA-Z0-9_-]', url):
    return False
else:
    return True

Answered By – Thilina Wimalapriya

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published