-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathregex.py
146 lines (90 loc) · 2.86 KB
/
regex.py
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
'''Python's regulax expression'''
import re
fil=open("dark.txt")
#search either dark or earth
for line in fil:
if re.search("dark|earth",line):
print(line,end='')
fil.close()
def tryo(filename):
with open(filename)as temp:
for t in temp:
findmatch=re.search("dark|earth|friend",t)
if findmatch:
print(findmatch.group())
#find the word and replace it with something
def replacegame(filename):
with open(filename) as temp:
for t in temp:
print(re.sub("earth","ho hala",t),end='')
#find the word and replace it with something and print only those case
def replacegame2(filename):
with open(filename) as temp:
for t in temp:
findmatch=re.search("earth",t)
if findmatch:
print(t.replace(findmatch.group(),"HAla bol"),end=' ')
def replacegame3(filename):
pattern=re.compile("earth")
with open(filename) as temp:
for t in temp:
findmatch=re.search(pattern,t)
if findmatch:
print(t.replace(findmatch.group(),"HAla bol"),end=' ')
gu='''Tell me not, in mournful numbers,
Life is but an empty dream! -
For the soul is dead that slumbers,
And things are not what they seem.
Life is real! Life is earnest!
And the grave is not its goal;
Dust thou art, to dust returnest,
Was not spoken of the soul.
Not enjoyment, and not sorrow,
Is our destined end or way;
But to act, that each tomorrow
Find us farther than today.
Art is long, and Time is fleeting,
And our hearts, though stout and brave,
Still, like muffled drums, are beating
Funeral marches to the grave.
In the world’s broad field of battle,
In the bivouac of Life,
Be not like dumb, driven cattle!
Be a hero in the strife!
Trust no Future, how’er pleasant!
Let the dead Past bury its dead!
Act, - act in the living Present!
Heart within, and God o’erhead!
Lives of great men all remind us
We can make our lives sublime,
And, departing, leave behind us
Footprints on the sands of time;
Footprints, that perhaps another,
Sailing o’er life’s solemn main,
A forlorn and shipwrecked brother,
Seeing, shall take heart again.
Let us, then, be up and doing,
With a heart for any fate;
Still achieving, still pursuing
Learn to labor and to walk. '''
emAIL="[email protected]"
num='958-594-511'
numm=re.compile(r'\d\d\d-\d\d\d-\d\d\d')
m=numm.finditer(num)
for k in m:
print(k)
pat=re.compile(r'[a-z,A-Z,0-9]@;.\w+',re.IGNORECASE)
mat=pat.finditer(emAIL)
print(mar for mar in mat)
print(x for match in mat)
replacegame3("dark.txt")
#replacegame2("dark.txt")
#replacegame("dark.txt")
pattern=re.compile(r'a/{1,3}b')
match=pattern.findall('a///b')
print(match)
pattern2=re.compile(r'\d+')
match2=pattern2.findall("James Bond 007i happiness 1/0")
print(match2)
print(re.findall(r'\d+','1232 dferf 1232'))
#tryo("dark.txt")