A set is a set of characters inside a pair of square brackets [] with a special meaning.
Set
Description
[arn]
Returns a match where one of the specified characters (a, r, or n) are present
import re
str="she sells sea shells on the sea shore"
#Check if the string has any a, r, or n characters:
x=re.findall("[arn]",str)
print(x)
if(x):
print("yes,there is atleast one match")
else:
print("no match")
==========o/p==========
['a', 'n', 'a', 'r']
yes,there is atleast one match
Set
Description
[a-n]
Returns a match for any lower case character, alphabetically between a and n
import re
str="She sells sea shells on the sea shore"
#Check if the string has any characters between a and n:
x=re.findall("[a-n]",str)
print(x)
if(x):
print("yes,there is atleast one match")
else:
print("no match")
==========O/P==============
['h', 'e', 'e', 'l', 'l', 'e', 'a', 'h', 'e', 'l', 'l', 'n', 'h', 'e', 'e', 'a', 'h', 'e']
yes,there is atleast one match
Set
Description
[^arn]
Returns a match for any character EXCEPT a, r, and n
import re
str="She sells sea shells on the sea shore"
#Check if the string has other characters than a, r, or n:
x=re.findall("[^arn]",str)
print(x)
if(x):
print("yes,there is atleast one match")
else:
print("no match")
============O/P=============
['S', 'h', 'e', ' ', 's', 'e', 'l', 'l', 's', ' ', 's', 'e', ' ', 's', 'h', 'e', 'l', 'l', 's', ' ', 'o', ' ', 't', 'h', 'e', ' ', 's', 'e', ' ', 's', 'h', 'o', 'e']
yes,there is atleast one match
Set
Description
[0123]
Returns a match where any of the specified digits (0, 1, 2, or 3) are present
import re
str="She sells sea shells on the sea shore"
#Returns a match where any of the specified digits (0, 1, 2, or 3) are present
x=re.findall("[0123]",str)
print(x)
if(x):
print("yes,there is atleast one match")
else:
print("no match")
===========o/p============
[]
no match
Set
Description
[0-9]
Returns a match for any digit between 0 and 9
import re
str="The time is 6:19 PM"
#Check if the string has any digits:
x=re.findall("[0-9]",str)
print(x)
if(x):
print("yes,there is atleast one match")
else:
print("no match")
===========O/P===========
['6', '1', '9']
yes,there is atleast one match
Set
Description
[0-5][0-9]
Returns a match for any two-digit numbers from 00 and 59
import re
str="The time is 18:19 PM"
#Check if the string has any two-digit numbers, from 00 to 59:
x=re.findall("[0-5][0-9]",str)
print(x)
if(x):
print("yes,there is atleast one match")
else:
print("no match")
==========O/P===========
['18', '19']
yes,there is atleast one match
Set
Description
[a-zA-Z]
Returns a match for any character alphabetically between a and z, lower case OR upper case
import re
str="The time is 18:19 PM"
#Check if the string has any characters from a to z lower case, and A to Z upper case:
x=re.findall("[a-zA-Z]",str)
print(x)
if(x):
print("yes,there is atleast one match")
else:
print("no match")
===========O/P=============
['T', 'h', 'e', 't', 'i', 'm', 'e', 'i', 's', 'P', 'M']
yes,there is atleast one match
Set
Description
[+]
In sets, +, *, .,(), $,{} has no special meaning, so [+] means: return a match for any + character in the string
import re
str="The time is 18:19 PM"
#Check if the string has any + characters:
x=re.findall("[+]",str)
print(x)
if(x):
print("yes,there is atleast one match")
else:
print("no match")
===========O/P============
[]
no match