0

My target is to match pattern like this:

'4d05'

but if the pattern is found, it should repeat exactly twice; like this:

'4d054d05'

What I have tried is:

pattern = re.compile(r'([0-9,a-f][0-9,a-f][0][1-5]){2}')

But I found that the above regex would also match pattern like:

'4d054005'

How should I modify my regex to eliminate the above false match?

mkrieger1
  • 10,793
  • 4
  • 39
  • 47
CodingNow
  • 875
  • 1
  • 8
  • 19
  • So my understanding is that I should modify my pattern to `pattern = re.compile(r'([0-9,a-f][0-9,a-f][0][1-5])\1')` – CodingNow May 29 '21 at 21:59
  • 1
    Additionally, if you're expecting only a `0` and nothing else in the third location, you could do without the `[` and `]` around it. – navneethc May 29 '21 at 22:01
  • I would also add an "I" flag in the second parameter to ignore case and accept either A5 or a5. – Chris Maurer May 30 '21 at 00:56

0 Answers0