The main problem of using them is that they difficult to understand, but they are well worth the effort to learn. Using a regular expression can save you a lot of time.

Lets start with a simple example:

Validating Input string

import re x="[a-z]+@[a-z]+\.[a-z]+" s1='liran@devarea.com' s2='liran#devarea.com' c=re.match(x,s1) if c: print ('ok') else: print ('no') 1 2 3 4 5 6 7 8 9 10 11 12 import re x = "[a-z]+@[a-z]+\.[a-z]+" s1 = 'liran@devarea.com' s2 = 'liran#devarea.com' c = re . match ( x , s1 ) if c : print ( 'ok' ) else : print ( 'no' )

We declare the regular expression to match email with a very simple rules:

at least one letter – [a-z]+

followed by @

followed by at least one letter

followed by period

followed by at least one letter

This is a very simple example , it doesn’t accept digits, doesn’t check for known extension (com/net/org) and there are more pitfalls. But the point is that if we want to add those rules we need to change the regular expression only.

Some match rules:

x? match 0 or 1 occurrences of x x+ match 1 or more occurrences of x x* match 0 or more occurrences of x x{m,n} match between m and n x’s hello match hello hello|world match hello or world ^ match beginning of text $ match end of text [a-zA-Z] match any char in the set [^a-zA-Z] match any char not in the set 1 2 3 4 5 6 7 8 9 10 11 12 x ? match 0 or 1 occurrences of x x + match 1 or more occurrences of x x * match 0 or more occurrences of x x { m , n } match between m and n x ’ s hello match hello hello | world match hello or world ^ match beginning of text $ match end of text [ a - zA - Z ] match any char in the set [ ^ a - zA - Z ] match any char not in the set

For example if we want to add support for digits in the first part of the email expression we add:

str="[a-z0-9]+@[a-z]+\.[a-z]+" 1 str = "[a-z0-9]+@[a-z]+\.[a-z]+"

Or if we want to enable only .com or .net emails we need to add:

x="[a-z0-9]+@[a-z]+\.(net|com)" 1 x = "[a-z0-9]+@[a-z]+\.(net|com)"

Some examples:

Email:

email="^[A-Za-z0-9\.\+_-]+@[A-Za-z0-9\._-]+\.[a-zA-Z]*$" 1 email = "^[A-Za-z0-9\.\+_-]+@[A-Za-z0-9\._-]+\.[a-zA-Z]*$"

URL