In [1]:
import re
In [2]:
# \d: Matches any digit (equivalent to [0-9])
import re
pattern = r"\d"
match = re.findall(pattern, "There are 2 apples and 5 oranges.")
print(match)
[''''2'''', ''''5'''']
In [3]:
# \D: Matches any non-digit (equivalent to [^0-9])
pattern = r"\D"
match = re.findall(pattern, "There are 2 apples and 5 oranges.")
print(match)
[''''T'''', ''''h'''', ''''e'''', ''''r'''', ''''e'''', '''' '''', ''''a'''', ''''r'''', ''''e'''', '''' '''', '''' '''', ''''a'''', ''''p'''', ''''p'''', ''''l'''', ''''e'''', ''''s'''', '''' '''', ''''a'''', ''''n'''', ''''d'''', '''' '''', '''' '''', ''''o'''', ''''r'''', ''''a'''', ''''n'''', ''''g'''', ''''e'''', ''''s'''', ''''.'''']
In [4]:
# \w: Matches any alphanumeric character (equivalent to [a-zA-Z0-9_])
pattern = r"\w"
match = re.findall(pattern, "Hello, World!")
print(match)
[''''H'''', ''''e'''', ''''l'''', ''''l'''', ''''o'''', ''''W'''', ''''o'''', ''''r'''', ''''l'''', ''''d'''']
In [5]:
# \W: Matches any non-alphanumeric character (equivalent to [^a-zA-Z0-9_])
pattern = r"\W"
match = re.findall(pattern, "Hello, World!")
print(match)
['''','''', '''' '''', ''''!'''']
In [6]:
# \s: Matches any whitespace character (equivalent to [ \t\n\r\f\v])
pattern = r"\s"
match = re.findall(pattern, "Hello World")
print(match)
['''' '''']
In [7]:
# \S: Matches any non-whitespace character (equivalent to [^ \t\n\r\f\v])
pattern = r"\S"
match = re.findall(pattern, "Hello World")
print(match)
[''''H'''', ''''e'''', ''''l'''', ''''l'''', ''''o'''', ''''W'''', ''''o'''', ''''r'''', ''''l'''', ''''d'''']
In [8]:
# [abc]: Matches any one of the characters a, b, or c
pattern = r"[abc]"
match = re.findall(pattern, "alphabet")
print(match)
[''''a'''', ''''a'''', ''''b'''']
In [9]:
# [^abc]: Matches any character except a, b, or c
pattern = r"[^abc]"
match = re.findall(pattern, "alphabet")
print(match)
[''''l'''', ''''p'''', ''''h'''', ''''e'''', ''''t'''']
In [10]:
# [a-z]: Matches any lowercase letter
pattern = r"[a-z]"
match = re.findall(pattern, "Hello World")
print(match)
[''''e'''', ''''l'''', ''''l'''', ''''o'''', ''''o'''', ''''r'''', ''''l'''', ''''d'''']
In [11]:
# [A-Z]: Matches any uppercase letter
pattern = r"[A-Z]"
match = re.findall(pattern, "Hello World")
print(match)
[''''H'''', ''''W'''']
In [12]:
# [0-9]: Matches any digit
pattern = r"[0-9]"
match = re.findall(pattern, "There are 2 apples and 5 oranges.")
print(match)
[''''2'''', ''''5'''']
In [13]:
# Matching Uppercase and Lowercase Letters
pattern = r"[A-Za-z]"
text = "Hello World 123"
matches = re.findall(pattern, text)
print(matches)
[''''H'''', ''''e'''', ''''l'''', ''''l'''', ''''o'''', ''''W'''', ''''o'''', ''''r'''', ''''l'''', ''''d'''']
In [14]:
import re
text = "The price is $123.45 on 2024-10-07."
pattern = r"\$\d+\.\d{2}"
matches = re.findall(pattern, text)
print(matches)
pattern = r"\d{4}-\d{2}-\d{2}"
matches = re.findall(pattern, text)
print(matches)
[''''$123.45''''] [''''2024-10-07'''']
In [15]:
import re
text = "User123 logged in at 10:45 AM on 2024-10-07."
pattern = r"\w+"
usernames = re.findall(pattern, text)
print(usernames)
pattern = r"\d{2}:\d{2}"
times = re.findall(pattern, text)
print(times)
pattern = r"\d{4}-\d{2}-\d{2}"
dates = re.findall(pattern, text)
print(dates)
[''''User123'''', ''''logged'''', ''''in'''', ''''at'''', ''''10'''', ''''45'''', ''''AM'''', ''''on'''', ''''2024'''', ''''10'''', ''''07''''] [''''10:45''''] [''''2024-10-07'''']