forked from AbeTavarez/Python_DevOps_Scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsplit_replace.py
More file actions
24 lines (16 loc) Β· 757 Bytes
/
Copy pathsplit_replace.py
File metadata and controls
24 lines (16 loc) Β· 757 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#!/usr/bin/env python3
import re
re_str = 'One sentence. Another sentence? And last sentence!'
# * Split
# split exclude split values
print(re.split(r'[.?!]', re_str))
# split includes split values
print(re.split(r'([.?!])', re_str))
# We want to split a piece of text by either the word "a" or "the", as implemented in the following code.What is the resulting split list?
print(re.split(r"the|a", "One sentence. Another one? And the last one!"))
#['One sentence. Ano', 'r one? And ', ' l', 'st one!']
# * Sub
re_str1 = 'Received an email for go_nuts95@my.exmaple.com'
print(re.sub(r'[\w.%+-]+@[\w.-]+', '[REDACTED FROM TEXT]', re_str1))
# Using capturing groups to reverse name
print(re.sub(r'^([\w .-]*), ([\w .-]*)$', r'\2 \1', 'Lovelace, Ada'))