PP - Chapter - 4
PP - Chapter - 4
Raw strings
Different functions in Python’s re module use raw string as an argument. A normal string,
when prefixed with 'r' or 'R' becomes a raw string.
Example: Raw String
>>> rawstr = r'Hello! How are you?'
>>> print(rawstr)
Hello! How are you?
The difference between a normal string and a raw string is that the normal string
in print() function translates escape characters (such as \n, \t etc.) if any, while those in a raw
string are not.
Example: String vs Raw String
str1 = "Hello!\nHow are you?"
print("normal string:", str1)
str2 = r"Hello!\nHow are you?"
print("raw string:",str2)
Output
normal string: Hello!
How are you?
raw string: Hello!\nHow are you?
In the above example, \n inside str1 (normal string) has translated as a newline being printed
in the next line. But, it is printed as \n in str2 - a raw string.
.^$*+?[]\|()
Metacharacters are characters with a special meaning:
| Either or "falls|stays"
When a set of alpha-numeric characters are placed inside square brackets [], the target
string is matched with these characters. A range of characters or individual characters
can be listed in the square bracket. For example:
Pattern Description
[abc] match any of the characters a, b, or c
[a-c] which uses a range to express the same set of characters.
[a-z] match only lowercase letters.
[0-9] match only digits.
Pattern Description
a| b Matches either a or b
re.match() function
This function in re module tries to find if the specified pattern is present at the beginning of
the given string.
re.match(pattern, string)
The function returns None, if the given pattern is not in the beginning, and a match objects if
found.
Example: re.match()
from re import match
re.findall() Function
As against the search() function, the findall() continues to search for the pattern till the target
string is exhausted. The object returns a list of all occurrences.
Example: re.findall()
re.finditer() function
The re.finditer() function returns an iterator object of all matches in the target string.
For each matched group, start and end positions can be obtained by span() attribute.
Example: re.finditer()
re.compile() Function
The re.compile() function returns a pattern object which can be repeatedly used in different
regex functions. In the following example, a string ‘is’ is compiled to get a pattern object and
is subjected to the search() method.
Example: re.compile()
from re import *
pattern = compile(r'[aeiou]')
string = "Flat is better than nested. Sparse is better than dense."
words = split(r' ', string)
for word in words:
print(word, pattern.match(word))
Output
Flat None
is <re.Match object; span=(0, 1), match='i'>
# compiling regex
match_re = re.compile(reg)
# searching regex
res = re.search(match_re, pswd)
# validating conditions
if res:
print("Valid Password")
else:
print("Invalid Password")
def check_url(ip_url):
regex = re.compile(
r'(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+(?:[A-Z]{2,6}\.?|[A-Z0-9-]{2,}\.?)|'
#domain...
r'localhost|' #localhost...
r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})' # ...or ip
r'(?:/?|[/?]\S+)$', re.IGNORECASE)
if (ip_url == None):
if(re.search(regex, ip_url)):
else:
ch = 'y'
while ch == 'y':
check_url(ip_url)
if ch == 'y':
continue
else:
break
Output:
if __name__ == '__main__':
# Enter the email
email = "[email protected]"
check(email)
email = "[email protected]"
check(email)
email = "ankitrai326.com"
check(email)
Output:
Valid Email
Valid Email
Invalid Email
List of Valid Email Addresses
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
email@[123.123.123.123]
"email"@example.com
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]