How to match exactly two same characters in a string like '4003', '1030'.
import re
s='1030'
if re.search('0{2}',s):
print(True)
But the above code matches only '1002' butnot '1030'
How to match exactly two same characters in a string like '4003', '1030'.
import re
s='1030'
if re.search('0{2}',s):
print(True)
But the above code matches only '1002' butnot '1030'
Assume you don't have to use regex:
Note that a string with 4 characters have exactly a pair of duplicating character if and only if it has 3 unique characters. So:
Do you HAVE to use regex
? Just use .count()
>>> '1002'.count('0')
2
>>> '1030'.count('0')
2
>>> '2002200220'.count('20')
3
This code sniped just checks if f.e. index 3 from the string number1
is equal to the index 3 from the string number2
.
number1 = '1002'
number2 = '1030'
counter = 0
for i in number1:
if number1[counter] is number2[counter]:
print("It's a match")
counter = counter + 1