Skip to content

Commit ecf941b

Browse files
committed
added regex tutorial
1 parent 497b01b commit ecf941b

File tree

6 files changed

+74
-0
lines changed

6 files changed

+74
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ This is a repository of all the tutorials of [The Python Code](https://www.thepy
9595
- [How to Read Emails in Python](https://www.thepythoncode.com/article/reading-emails-in-python). ([code](python-standard-library/reading-email-messages))
9696
- [How to Download and Upload Files in FTP Server using Python](https://www.thepythoncode.com/article/download-and-upload-files-in-ftp-server-using-python). ([code](python-standard-library/download-and-upload-files-in-ftp))
9797
- [How to Work with JSON Files in Python](https://www.thepythoncode.com/article/working-with-json-files-in-python). ([code](python-standard-library/working-with-json))
98+
- [How to Use Regular Expressions in Python](https://www.thepythoncode.com/article/work-with-regular-expressions-in-python). ([code](python-standard-library/regular-expressions))
9899

99100
- ### [Using APIs](https://www.thepythoncode.com/topic/using-apis-in-python)
100101
- [How to Automate your VPS or Dedicated Server Management in Python](https://www.thepythoncode.com/article/automate-veesp-server-management-in-python). ([code](general/automating-server-management))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# [How to Use Regular Expressions in Python](https://www.thepythoncode.com/article/work-with-regular-expressions-in-python)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import re
2+
3+
# fake ipconfig output
4+
example_text = """
5+
Ethernet adapter Ethernet:
6+
7+
Media State . . . . . . . . . . . : Media disconnected
8+
Physical Address. . . . . . . . . : 88-90-E6-28-35-FA
9+
10+
Ethernet adapter Ethernet 2:
11+
12+
Physical Address. . . . . . . . . : 04-00-4C-4F-4F-60
13+
Autoconfiguration IPv4 Address. . : 169.254.204.56(Preferred)
14+
15+
Wireless LAN adapter Local Area Connection* 2:
16+
17+
Media State . . . . . . . . . . . : Media disconnected
18+
Physical Address. . . . . . . . . : B8-21-5E-D3-66-98
19+
20+
Wireless LAN adapter Wi-Fi:
21+
22+
Physical Address. . . . . . . . . : A0-00-79-AA-62-74
23+
IPv4 Address. . . . . . . . . . . : 192.168.1.101(Preferred)
24+
Default Gateway . . . . . . . . . : 192.168.1.1
25+
"""
26+
# regex for MAC address
27+
mac_address_regex = r"([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})"
28+
# iterate over matches and extract MAC addresses
29+
extracted_mac_addresses = [ m.group(0) for m in re.finditer(mac_address_regex, example_text) ]
30+
print(extracted_mac_addresses)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import re # stands for regular expression
2+
# a regular expression for validating a password
3+
match_regex = r"^(?=.*[0-9]).{8,}$"
4+
# a list of example passwords
5+
passwords = ["pwd", "password", "password1"]
6+
for pwd in passwords:
7+
m = re.match(match_regex, pwd)
8+
print(f"Password: {pwd}, validate password strength: {bool(m)}")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import re
2+
3+
# part of ipconfig output
4+
example_text = """
5+
Wireless LAN adapter Wi-Fi:
6+
7+
Connection-specific DNS Suffix . :
8+
Link-local IPv6 Address . . . . . : fe80::380e:9710:5172:caee%2
9+
IPv4 Address. . . . . . . . . . . : 192.168.1.100
10+
Subnet Mask . . . . . . . . . . . : 255.255.255.0
11+
Default Gateway . . . . . . . . . : 192.168.1.1
12+
"""
13+
14+
# regex for IPv4 address
15+
ip_address_regex = r"((25[0-5]|(2[0-4]|1[0-9]|[1-9]|)[0-9])(\.(?!$)|$)){4}"
16+
# use re.search() method to get the match object
17+
match = re.search(ip_address_regex, example_text)
18+
print(match)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import re
2+
3+
# a basic regular expression for email matching
4+
email_regex = r"[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+"
5+
# example text to test with
6+
example_text = """
7+
Subject: This is a text email!
8+
From: John Doe <[email protected]>
9+
Some text here!
10+
===============================
11+
Subject: This is another email!
12+
From: Abdou Rockikz <[email protected]>
13+
Some other text!
14+
"""
15+
# substitute any email found with [email protected]
16+
print(re.sub(email_regex, "[email protected]", example_text))

0 commit comments

Comments
 (0)