Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitecf941b

Browse files
committed
added regex tutorial
1 parent497b01b commitecf941b

File tree

6 files changed

+74
-0
lines changed

6 files changed

+74
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
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))
Lines changed: 1 addition & 0 deletions
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)
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
importre
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)forminre.finditer(mac_address_regex,example_text) ]
30+
print(extracted_mac_addresses)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
importre# 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+
forpwdinpasswords:
7+
m=re.match(match_regex,pwd)
8+
print(f"Password:{pwd}, validate password strength:{bool(m)}")
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
importre
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)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
importre
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 <john@doe.com>
9+
Some text here!
10+
===============================
11+
Subject: This is another email!
12+
From: Abdou Rockikz <example@domain.com>
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)

[8]ページ先頭

©2009-2025 Movatter.jp