-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathregex_sub.py
123 lines (89 loc) · 2.38 KB
/
regex_sub.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
"""
The re.sub() tool (sub stands for substitution) evaluates a pattern and, for each valid match, it calls a method (or lambda).
The method is called for all matches and can be used to modify strings in different ways.
The re.sub() method returns the modified string as an output.
Learn more about .
Transformation of Strings
Code
import re
#Squaring numbers
def square(match):
number = int(match.group(0))
return str(number**2)
print re.sub(r"\d+", square, "1 2 3 4 5 6 7 8 9")
Output
1 4 9 16 25 36 49 64 81
Replacements in Strings
Code
import re
html = """
<head>
<title>HTML</title>
</head>
<object type="application/x-flash"
data="your-file.swf"
width="0" height="0">
<!-- <param name="movie" value="your-file.swf" /> -->
<param name="quality" value="high"/>
</object>
"""
print re.sub("(<!--.*?-->)", "", html) #remove comment
Output
<head>
<title>HTML</title>
</head>
<object type="application/x-flash"
data="your-file.swf"
width="0" height="0">
<param name="quality" value="high"/>
</object>
Task
You are given a text of lines. The text contains && and || symbols.
Your task is to modify those symbols to the following:
&& → and
|| → or
Both && and || should have a space " " on both sides.
Input Format
The first line contains the integer, .
The next lines each contain a line of the text.
Constraints
Neither && nor || occur in the start or end of each line.
Output Format
Output the modified text.
Sample Input
11
a = 1;
b = input();
if a + b > 0 && a - b < 0:
start()
elif a*b > 10 || a/b < 1:
stop()
print set(list(a)) | set(list(b))
#Note do not change &&& or ||| or & or |
#Only change those '&&' which have space on both sides.
#Only change those '|| which have space on both sides.
Sample Output
a = 1;
b = input();
if a + b > 0 and a - b < 0:
start()
elif a*b > 10 or a/b < 1:
stop()
print set(list(a)) | set(list(b))
#Note do not change &&& or ||| or & or |
#Only change those '&&' which have space on both sides.
#Only change those '|| which have space on both sides.
"""
# Enter your code here. Read input from STDIN. Print output to STDOUT
import re
T = []
def change(match):
if match.group(0) == '&&':
return 'and'
elif match.group(0) == '||':
return 'or'
N = input()
for i in xrange(N):
T.append(raw_input())
t = '\n'.join(T)
print re.sub(r"(?<= )(&&|\|\|)(?= )", change, t)