regex substitution - Regular Expressions - HackerRank - #13 - Python Practice Examples

Veröffentlicht am: 09 Mai 2020
auf dem Kanal: Interview Coder
468
5

This Challange is to test the logic of regex substitution in HackerRank.

Join :
  / jagwithyou  
  / jagwithyou  
https://github.com/jagwithyou



Hint:
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


#CompetitiveProgramming #TWP


Auf dieser Seite können Sie das Online-Video regex substitution - Regular Expressions - HackerRank - #13 - Python Practice Examples mit der Dauer stunde minuten sekunde in guter Qualität ansehen, das der Benutzer Interview Coder 09 Mai 2020 hochgeladen hat, den Link mit Freunden und Bekannten teilen, dieses Video wurde auf Youtube bereits 468 Mal angesehen und es wurde von 5 den Zuschauern gefallen. Viel Spaß beim Betrachtenden Zuschauern gefallen!