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)

27th Dec 2020, 2:37 AM
Kevin Grothe
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
27th Dec 2020, 3:00 AM
noteve
noteve - avatar