-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRE.py
195 lines (114 loc) · 3.13 KB
/
RE.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#!/usr/bin/env python
# coding: utf-8
# In[1]:
import re
# In[16]:
#to find any string starting with x and ending with y.
x,y=input().split()
test_string = input()
n=len(test_string)
pattern ='^'+x+'.'*(n-2)+y+'$'
print(pattern)
result = re.match(pattern, test_string)
if result:
print("Search successful.")
else:
print("Search unsuccessful.")
# In[17]:
# Program to extract numbers from a string
string = input('Enter Your text to print all the numbers in your text:')
pattern = '\d+'
result = re.findall(pattern, string)
print(result)
# In[18]:
#The re.split method splits the string where there is a match
#and returns a list of strings where the splits have occurred.
string = input()
pattern = '\d+'
result = re.split(pattern, string)
print(result)
# In[23]:
#replace:
import re
# multiline string
string =input()
# matches all whitespace characters
pattern = input('Enter word you want to change:')
# empty string
replace = input('Enter the new word:')
new_string = re.sub(pattern,replace, string)
print(new_string)
# In[24]:
# url link
s =input()
# finding the protocol
obj1 = re.findall('(\w+)://',s)
print('Protocol:',obj1)
# finding the hostname which may
# contain dash or dots
obj2 = re.findall('://([\w\-\.]+)',s)
print('Host:',obj2)
# In[29]:
#validating IP
regex = '''^(25[0-5]|2[0-4][0-9]|[0-1]?[0-9][0-9]?)\.(
25[0-5]|2[0-4][0-9]|[0-1]?[0-9][0-9]?)\.(
25[0-5]|2[0-4][0-9]|[0-1]?[0-9][0-9]?)\.(
25[0-5]|2[0-4][0-9]|[0-1]?[0-9][0-9]?)$'''
Ip=input('Enter a IP address: ')
if(re.search(regex, Ip)):
print("Valid Ip address")
else:
print("Invalid Ip address")
# In[30]:
regex = '^[a-z0-9]+[\._]?[a-z0-9]+[@]\w+[.]\w{2,3}$'
email=input('Enter a mail id: ')
if(re.search(regex,email)):
print("Valid Email")
else:
print("Invalid Email")
# In[32]:
string =input('Enter your text : ')
print(re.findall(r'"(.*?)"', string))
# In[33]:
#Search for the first white-space character in the string:
import re
txt = input()
x = re.search("\s", txt)
print("The first white-space character is located in position:", x.start())
# In[39]:
#Make a search that returns no match:
txt = input()
k=input('enter the word you want to search : ')
x=re.search(k, txt)
if x:
print('The word is in the text at position :',x.span())
else:
print("The word is not found")
# In[2]:
#Text
# In[ ]:
#replace
with open("C:\\Users\\Randrita s\\Downloads\\cow1.txt") as f:
content = f.readlines()
content = [x.strip() for x in content]
#print(content)
pattern = input('Enter word you want to change:')
# empty string
replace = input('Enter the new word:')
for i in content:
#if i==pattern:
new_string= re.sub(pattern,replace,i)
print(new_string)
# In[ ]:
#search
with open("C:\\Users\\Randrita s\\Downloads\\cow1.txt") as f:
content = f.readlines()
content = [x.strip() for x in content]
#print(content)
pattern = input('Enter word you want to change:')
# empty string
replace = input('Enter the new word:')
for i in content:
#if i==pattern:
new_string= re.sub(pattern,replace,i)
print(new_string)