Regex Substitution
Create Date: March 31, 2019 at 04:04 PM         | Tag: PYTHON         | Author Name: Sun, Charles |
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.
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.
import re
for i in range(int(input())):
print(re.sub(r'(?<= )(&&|\|\|)(?= )', lambda x: 'and' if x.group() == '&&' else 'or', input()))
New Comment
Re.start() & Re.end()
Create Date: March 31, 2019 at 03:57 PM         | Tag: PYTHON         | Author Name: Sun, Charles |
start() & end()
These expressions return the indices of the start and end of the substring matched by the group.
Code
>>> import re
>>> m = re.search(r'\d+','1234')
>>> m.end()
4
>>> m.start()
0
Task
You are given a string .
Your task is to find the indices of the start and end of string in .
Input Format
The first line contains the string .
The second line contains the string .
Constraints
Output Format
Print the tuple in this format: (start _index, end _index).
If no match is found, print (-1, -1)
.
Sample Input
aaadaa
aa
Sample Output
(0, 1)
(1, 2)
(4, 5)
import re
s, k = input(), input()
pattern = re.compile(k)
m = pattern.search(s)
if not m: print("(-1, -1)")
while m:
print("({0}, {1})".format(m.start(), m.end() - 1))
m = pattern.search(s, m.start() + 1)
New Comment
Re.findall() & Re.finditer()
Create Date: March 31, 2019 at 03:50 PM         | Tag: PYTHON         | Author Name: Sun, Charles |
re.findall()
The expression re.findall() returns all the non-overlapping matches of patterns in a string as a list of strings.
Code
>>> import re
>>> re.findall(r'\w','http://www.hackerrank.com/')
['h', 't', 't', 'p', 'w', 'w', 'w', 'h', 'a', 'c', 'k', 'e', 'r', 'r', 'a', 'n', 'k', 'c', 'o', 'm']
re.finditer()
The expression re.finditer() returns an iterator yielding MatchObject
instances over all non-overlapping matches for the repattern in the string.
Code
>>> import re
>>> re.finditer(r'\w','http://www.hackerrank.com/')
<callable-iterator object at 0x0266C790>
>>> map(lambda x: x.group(),re.finditer(r'\w','http://www.hackerrank.com/'))
['h', 't', 't', 'p', 'w', 'w', 'w', 'h', 'a', 'c', 'k', 'e', 'r', 'r', 'a', 'n', 'k', 'c', 'o', 'm']
Task
You are given a string . It consists of alphanumeric characters, spaces and symbols(+
,-
).
Your task is to find all the substrings of that contains or more vowels.
Also, these substrings must lie in between consonants and should contain vowels only.
Note :
Vowels are defined as: AEIOU
and aeiou
.
Consonants are defined as: QWRTYPSDFGHJKLZXCVBNM
and qwrtypsdfghjklzxcvbnm
.
Input Format
A single line of input containing string .
Constraints
Output Format
Print the matched substrings in their order of occurrence on separate lines.
If no match is found, print -1
.
Sample Input
rabcdeefgyYhFjkIoomnpOeorteeeeet
Sample Output
ee
Ioo
Oeo
eeeee
Explanation
ee is located between consonant and .
Ioo is located between consonant and .
Oeo is located between consonant and .
eeeee is located between consonant and .
method 1:
import re
v = "aeiou"
c = "qwrtypsdfghjklzxcvbnm"
m = re.findall(r"(?<=[%s])([%s]{2,})[%s]" % (c, v, c), input(), flags = re.I)
print("\n".join(m or ["-1"]))
(?=...)
Matches if ... matches next, but doesn’t consume any of the string. This is called a lookahead assertion. For example, Isaac (?=Asimov) will match 'Isaac ' only if it’s followed by 'Asimov'.
(?<=...)
Matches if the current position in the string is preceded by a match for ... that ends at the current position. This is called a positive lookbehind assertion. (?<=abc)def will find a match in abcdef, since the lookbehind will back up 3 characters and check if the contained pattern matches.
method 2:
import re
S = input()
regex = r"[^AEIOUaeiou]?[AEIOUaeiou]{2,}[^AEIOUaeiou]{1}"
pattern = re.compile(regex)
matches = list(map(lambda x: x.group(), pattern.finditer(S)))
if matches:
for match in matches:
vowels = r"[AEIOUaeiou]{2,}"
result = re.compile(vowels).search(match).group()
print(result)
else:
print('-1')
New Comment