1

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'

NSVR
  • 192
  • 1
  • 2
  • 12
  • 1
    Must you use regex? – user202729 May 17 '18 at 11:10
  • You're basically doing the opposite of [this](https://stackoverflow.com/questions/12870489/regex-to-match-a-word-with-unique-non-repeating-characters). – Aran-Fey May 17 '18 at 11:14
  • 1
    It would be helpful if you could provide additional context such as: 1) Is the length of the string always 4? 2) Does it have to be any 2 characters or ones that you can specifically hardcode (for example, specifically for 0, etc) – sshashank124 May 17 '18 at 11:18
  • 2
    Does a word like `aabb` match the criteria of "exactly 2 same characters" or not? Depending on how you look at it, that could be construed as "4 identical characters". – Aran-Fey May 17 '18 at 11:25

3 Answers3

1

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:

  • Make a set of its characters
  • Check if there are 3 distinct elements in the set.
user202729
  • 3,358
  • 3
  • 25
  • 36
  • 1
    You can't use a set to determine if a character appears *exactly* twice. For example, both `ab` and `abb` would be turned into `{'a', 'b'}`. – Aran-Fey May 17 '18 at 11:18
  • @Aran-Fey That's the point of the explanation. "Note that a string with 4 characters have exactly a pair of duplicating character if and only if it has 3 unique characters. ". I don't directly check for "a character appear exactly twice", I check for "there are 3 distinct characters". – user202729 May 17 '18 at 11:19
  • Well it's still wrong because `aabb` matches the criteria and doesn't have 3 distinct characters. – Aran-Fey May 17 '18 at 11:19
  • @Aran-Fey OP said that "exactly two same characters" ... well I guess flag to close the question is better. That can be interpreted in multiple different ways. – user202729 May 17 '18 at 11:20
0

Do you HAVE to use regex? Just use .count()

>>> '1002'.count('0')
2
>>> '1030'.count('0')
2
>>> '2002200220'.count('20')
3
sshashank124
  • 31,495
  • 9
  • 67
  • 76
  • 2
    OP doesn't say that the duplicate character is guaranteed to be 0. – user202729 May 17 '18 at 11:15
  • 1
    Hmmm I thought that too, but their example has '0{2}' hardcoded so I figured this was specifically their use case. Let's wait on clarification from the user – sshashank124 May 17 '18 at 11:16
  • My requirement is,able to find exactly two zeros in a 4 digit number. Can you give me regex slolution? – NSVR May 18 '18 at 08:07
0

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
Laurent Mouchart
  • 216
  • 1
  • 3
  • 13