304 North Cardinal St.
Dorchester Center, MA 02124

# Regex Substitution Hacker Rank Solution – Queslers

## Problem : Regex Substitution Hacker Rank Solution

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 = """
<title>HTML</title>
<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>
<object type="application/x-flash"
data="your-file.swf"
width="0" height="0">

<param name="quality" value="high"/>
</object>
``````

You are given a text of N lines. The text contains && and || symbols.

```&& → and
|| → or
```

Both && and || should have a space ” ” on both sides.

#### Input Format :

The first line contains the integer, N.
The next N lines each contain a line of the text.

#### Constraints :

• 0 < N < 100
• 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.``````

### Regex Substitution Hacker Rank Solution in python 2

``````import re

n = int(raw_input())
for _ in range(0, n):
line = raw_input()
line = re.sub(r'\ [\&]{2,2}\ ', ' and ', line)
line = re.sub(r'\ [\|]{2,2}\ ', ' or ', line)
line = re.sub(r'\ [\&]{2,2}\ ', ' and ', line)
line = re.sub(r'\ [\|]{2,2}\ ', ' or ', line)
print(line)``````

### Regex Substitution Hacker Rank Solution in python 3

``````import re

ii = int(input())

for i in range(0,ii):
txt = input()
txt = re.sub(r"\ \&\&\ "," and ",txt)
txt = re.sub(r"\ \|\|\ "," or ",txt)
txt = re.sub(r"\ \&\&\ "," and ",txt)
txt = re.sub(r"\ \|\|\ "," or ",txt)
print(txt)
``````

### Regex Substitution Hacker Rank Solution in pypy

``````# Enter your code here. Read input from STDIN. Print output to STDOUT
import sys
import re

pat = re.compile(r'''(?<= )(&&|\|\|)(?= )''', re.U|re.I|re.M|re.S)

for idx in range(int(raw_input())):
l = raw_input()
if pat.search(l):
print(pat.sub(lambda m: 'and' if m.group(1) =='&&' else 'or', l))
else:
print(l)``````

### Regex Substitution Hacker Rank Solution in pypy 3

``````import re

for _ in range(int(input())):
str_ = input()
str_ = re.sub(r"(?<= )(&&)(?= )", "and", str_)
print(re.sub(r"(?<= )(\|\|)(?= )", "or", str_))``````
##### Regex Substitution Hacker Rank Solution Review:

In our experience, we suggest you solve this Regex Substitution Hacker Rank Solution and gain some new skills from Professionals completely free and we assure you will be worth it.

Regex Substitution is available on Hacker Rank for Free, if you are stuck anywhere between compilation, just visit Queslers to get all Hacker Rank Solution

##### Regex Substitution Hacker Rank Solution Conclusion:

I hope this Regex Substitution Hacker Rank Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Hacker Rank, Leetcode, Codechef, Codeforce Solution.

This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.

Keep Learning!

More Hacker Rank Problem & Solutions >>

Mini-Max Sum Hacker Rank Solution

String Validators Hacker Rank Solution

Text Alignment Hacker Rank solution

String validators Hacker Rank Solution