Ladder game string replacement question "6 turned over" Python regular expression replacement

Input format: Input a sentence in one line, that is, a non-empty string consisting of no more than 1000 English letters, numbers and spaces, and end with a carriage return.

Output format: Scan the input sentence from left to right: If there are more than 3 consecutive 6s in the sentence, replace this string of 6s with 9; but if there are more than 9 consecutive 6s, then replace this string of consecutive 6s 6 is replaced by 27. Other contents are not affected and are output as is.

Input sample:
it is so 666 really 6666 what else can I say 6666666666
Output sample:
it is so 666 really 9 what else can I say 27

Problem-solving idea: first match more than 9 consecutive 6s and replace them with 27 and then match more than 3 consecutive 6s and replace them with 9

Accepted code:

import re
print(re.sub(r'6{4,}','9',re.sub(r'6{10,}','27',input())))

Call the sub method of the re library

Basic metacharacters of regular expressions:
Regular expression

Guess you like

Origin blog.csdn.net/weixin_56336619/article/details/115211490