-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssignment15.py
More file actions
57 lines (45 loc) · 2.15 KB
/
Assignment15.py
File metadata and controls
57 lines (45 loc) · 2.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#(Q.1)- Extract the user id, domain name and suffix from the following email addresses.
# desired_output = [('zuck26', 'facebook', 'com'), ('page33', 'google', 'com'), ('jeff42', 'amazon', 'com')].
print("Ans.(1)->")
import re
p = re.compile(r"[A-Za-z0-9A-Za-z][A-za-z0-9A-za-z][A-za-z0-9A-za-z]+")
result = p.findall(email)
print(result)
print( )
#(Q.2)- Retrieve all the words starting with ‘b’ or ‘B’ from the following text.
#text = "Betty bought a bit of butter, But the butter was so bitter, So she bought some better butte
print("Ans.(2)->")
text = "Betty bought a bit of butter, But the butter was so bitter, So she bought some better butte"
p = re.compile(r"B[A-za-z]+")
result = p.findall(text)
print(result)
result = p.finditer(text)
for r in result:
print(r)
print()
p = re.compile(r"b[A-za-z]+")
result = p.findall(text)
print(result)
result = p.finditer(text)
for r in result:
print(r)
print()
#(Q.3)- Split the following irregular sentence into words
# sentence = "A, very very; irregular_sentence"
# desired_output = "A very very irregular sentence"
print("Ans.(3)->")
text = "A very very irregular_sentence"
p = re.sub(r"_"," ",text)
print(p)
print( )
# ***Optional Question***
#(Q.4)- Clean up the following tweet so that it contains only the user’s message. That is, remove all URLs, hashtags, mentions, punctuations, RTs and CCs.
#tweet = "Good advice! RT @TheNextWeb: What I would do differently if I was learning to code today http://t.co/lbwej0pxOd cc: @garybernhardt #rstats"
#desired_output = 'Good advice What I would do differently if I was learning to code today'
print("Ans.(4)->")
tweet = "Good advice! RT @TheNextWeb: What I would do differently if I was learning to code today http://t.co/lbwej0pxOd cc: @garybernhardt #rstats"
p = re.sub(r"Good advice! RT @TheNextWeb: What I would do differently if I was learning to code today http://t.co/lbwej0pxOd cc: @garybernhardt #rstats",
"'Good advice What I would do differently if I was learning to code today'",tweet)
print(p)