Skip to content

Commit 3bc0bd9

Browse files
committed
scripts and regex
1 parent 486dd57 commit 3bc0bd9

8 files changed

+129
-0
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import re
2+
3+
statement = "Please contact us at: [email protected], [email protected]"
4+
5+
pattern = re.compile(r"""
6+
[\w\.-]+@[\w\.-]+
7+
""", re.X | re.I)
8+
9+
addresses = re.findall(pattern, statement)
10+
for address in addresses:
11+
print("Address: ", address)

__pycache__/webbrowser.cpython-39.pyc

202 Bytes
Binary file not shown.

featch request.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import urllib.request
2+
3+
url = "https://www.bing.com/search?q=python"
4+
headers ={}
5+
headers['User-Agent'] = "Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.27 Safari/537.17"
6+
req = urllib.request.Request(url, headers= headers)
7+
8+
data =urllib.request.urlopen(req)
9+
print(data.read())

regex intermediate.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import re
2+
3+
4+
statement = 'Please contact us at: [email protected]'
5+
match = re.search(r'([\w-]+)@([\w-]+)', statement)
6+
if statement:
7+
print("Email address:", match.group())
8+
print("Username:", match.group(1))
9+
print("Host:", match.group(2))
10+
11+
12+
# you can also create named
13+
14+
statement = 'Please contact us at: [email protected]'
15+
match2 = re.search(r'(?P<email>(?P<username>[\w-]+)@(?P<host>[\w-]+))', statement)
16+
if statement:
17+
print("Email address:", match2.group("email"))
18+
print("Username:", match2.group("username"))
19+
print("Host:", match2.group("host"))
20+
21+
22+
# compliling demo match string into regular expression object
23+
24+
pattern = re.compile(r'(?P<email>(?P<username>[\w-]+)@(?P<host>[\w-]+))')
25+
26+
match3 = pattern.search(statement)
27+
28+
if statement:
29+
print("Email address:", match3.group("email"))
30+
print("Username:", match3.group("username"))
31+
print("Host:", match3.group("host"))
32+
33+
34+
#findall in re
35+
36+
statement = "Please contact us at: [email protected], [email protected]"
37+
38+
emails = re.findall(r'[\w\.-]+@[\w\.-]+', statement)
39+
40+
# Finds all the possible matches in the entire sequence and returns them
41+
# as a list of strings. Each returned string represents one match.
42+
for email in emails:
43+
print(email)
44+
45+
46+
47+
pattern = re.compile('COOKIE', re.IGNORECASE)
48+
match = pattern.search("I am not a cookie monster")
49+
50+
print("Start index:", match.start())
51+
print("End index:", match.end())
52+
print("Tuple:", match.span())

regex.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import re
2+
3+
print(re.findall(r'quant[ia]tative','will find either quantitative, or quantatative.'))
4+
5+
exampleString = '''Jessica is 15 years old, and Daniel is 27 years old.
6+
Edward is 97 years old, and his grandfather. (do did done)
7+
'''
8+
9+
ages = re.findall(r'\d{1,2}',exampleString)
10+
names = re.findall(r'[A-Z][a-z]*',exampleString)
11+
print(ages)
12+
print(names)
13+
14+
# everything except abc,\n
15+
print(re.findall(r'[^abc(^\n)]',exampleString))
16+
17+
#word after d
18+
print(re.findall(r'(d)(\w+)',exampleString))
19+
20+
#dxxxxxxxxx
21+
#dxxxxxxxxd
22+
print(re.findall(r'(d)(\w+)(d)',exampleString))
23+
24+
print(re.search(r'(d)(\w+)(d)',exampleString)) #returns the re object
25+
print(re.search(r'(d)(\w+)(d)',exampleString).group()) # returns the matches

shoutdown like a pro.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import os
2+
import time
3+
shutdown = input("Do you wish to shutdown your computer ? (yes / no): ")
4+
5+
if shutdown == 'y' or shutdown == 'Y':
6+
info = input(" Now (yes / no): ")
7+
if info == "y" or info=="Y":
8+
print("Y")
9+
os.system("shutdown /s /t 1")
10+
print(f"Bye Bye")
11+
time.sleep(1)
12+
else:
13+
info2 = int(input("How many minuets leter ? ") )
14+
try:
15+
print(f"You will be shutdown after {info2} minuets")
16+
os.system(f"shutdown /s /t {info2*60}")
17+
time.sleep(1)
18+
except:
19+
print(f"Input error or you changed mind? please try leter")
20+
time.sleep(2)
21+
else:
22+
exit()

simple agrument from terminal.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import sys
2+
3+
try:
4+
print(f"You wrote in terminal: {sys.argv[1]}, {sys.argv[2]}")
5+
except:
6+
print("You don't give input in terminal")

sys.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import sys
2+
3+
print(f"This is the biggest integer {sys.maxsize}")
4+
print(f"Your platform or OS is {sys.platform}")

0 commit comments

Comments
 (0)