0
Regular expression canât solve - one bug in hidden test
(NOT SURE WHY ALL CAPS) THE PROGRAM IS SUPPOSED TO TAKE OFF THE FIRST TWO â00â AND REPLACE WITH A â+â IN A PHONE NUMBER BUT THERES ONE TEST THAT WONT WORK IN THE CODE COACH AND ITS HIDDEN SO I CANâT TELL WHAT THE PROBLEM IS import re phone = input() pattern = r'00' match = re.search(pattern,phone) if match.start() == 0 and match.end() == 2: new = re.sub(pattern, '+', phone) print(new) else: print(phone)
1 RĂ©ponse
+ 1
pattern = r"^00" instead of pattern = "00"
^ means start and so that it will only change the 00 in the beginning instead of all 00.
- - - - - - - - - - - - -
INPUT:
0014800132
r"00" - - -> +148+132
r"^00" - - -> +14800132
- - - - - - - - - - - - - - - - - - - - -
Another thing,
match = re.match(pattern, phone)
Use re.match instead of re.search so it will start checking the string from left to right rather than all of it at the same time.
- - - - - - - - - - - - - - - - - - - -
And another cause of error is when the number does not start with 00 the "match" variable will have 'None' value which will cause an error in if-else statement.
I hope this helps. Please update me if the problem is still not solved. Thanks and Happy Coding!
Code:
https://code.sololearn.com/cA144A149a6a
https://code.sololearn.com/c0a269A4a160