Lecture 4
Lecture 4
3/
RegExs In Python
Based on https://docs.python.org/3.6/howto/regex.html
Used to specify rules to match patterns in strings
Like Unix’s grep
Also, can be used to substitute strings
Like Unix’s sed
import re
l = re.findall(r'\d', 'we23f345rfsfewt5r')
print(l) # outputs ['2', '3', '3', '4', '5',
'5'] l = re.findall(r'[0-9]', 'we23f345rfsfewt5r')
print(l) # same as above
l = re.findall(r'ca*t', 'ewewrctcatcaatcaaaaaaatcg')
#
l >= 0 times
= re.findall(r'ca?t','ewewrctcatcaatcaaaaaaatcg') # 0 or 1 times
print(l)
print(l) # outputs
# outputs ['ct',
['ct', 'cat', 'caat',
'cat']
'caaaaaaat'] 4 / 12
RegExs In Python
File = """
[email protected] wrer@efe@fe @r4etre.com
dfraeqw@
wrere.org [email protected]
"""
l = re.findall(r'[a-zA-
Z0-9_\-.]+@[a-zA-Z0-
9_\-]+\.[a-zA-Z]+',
File)
print(l) #
outputs ['[email protected]',
'[email protected]']
l = re.findall(r'[a-zA-
Z0-9_\-.]+@[a-zA-Z0-
9_\-]+\.[a-zA-Z0-9\-.]
+', File)
print(l) #
outputs ['[email protected]', 5 / 12
Network Programming (High Level): Interacting with the Web
6 / 12
Network Programming (High Level): Interacting with the Web
#!/usr/bin/python3.6
import requests
request_url = "https://www.google.com/search"
f = open('google.html', 'w')
f.write(response.text)
7 / 12
Safari Web Developer Tools: Web Inspector
8 / 12
Network Programming (High Level): Interacting with the Web
ahmad@ubuntu:~/NES470$ ./Requests.py
Status Code: 200
Complete URL: https://www.google.com/search?client=safari&source=hp&
q=Regular+Expression&sclient=psy-ab
Encoding: ISO-8859-1
Response Headers:
{'Content-Type':
'text/html;
charset=ISO-8859-1',
'Date': 'Fri, 25
...
ma=2592000;
v="46,43"',
'Transfer-Encoding':
'chunked'}
9 / 12
Network Programming (High Level): Interacting with the Web
#!/usr/bin/python3.6
import requests
import re
request_url = "https://www.google.com/search"
for u in urls:
print("{:2d}: {}".format(urls.index(u) + 1, u))
10 /
Safari Web Developer Tools: Web Inspector
11 /
Network Programming (High Level): Interacting with the Web
ahmad@ubuntu:~/NES470$ ./Requests-RE.py
1: https://en.wikipedia.org/wiki/Regular_expression
2: https://developer.mozilla.org/ar/docs/Web/JavaScript/Guide/
Regular_Expressions
3: https://regexr.com/
4: https://docs.python.org/3/library/re.html
5: https://docs.python.org/3/howto/regex.html
6: https://www.w3schools.com/jsref/jsref_obj_regexp.asp
7: https://www.w3schools.com/js/js_regexp.asp
8: https://www.regular-expressions.info/
9: https://www3.ntu.edu.sg/home/ehchua/programming/howto/Regexe.
html
10: https://support.google.com/analytics/answer/1034324%3Fhl%3Den
12 /