0

I'm trying to match iff two capture groups are the same. I could manually check after the match, but I'm wondering if there is a way I can do this in the expression itself.

My expression is (\d+)\/(\d+), so I only want to accept strings where the two numbers are equal. Is there a nice way to check this in the regular expression, or do I have to manually check groups after?

EDIT: This was marked a duplicate but the supposed duplicate question is not related and does not in any way answer my question...

chris
  • 4,840
  • 5
  • 35
  • 66

2 Answers2

2

You can use this one in python : \b(\d+)\/+\1\b

Demo

This is the same usecase as checking for doubled words

When editing text, doubled words such as "the the" easily creep in. Using the regex \b(\w+)\s+\1\b in your text editor, you can easily find them. To delete the second word, simply type in \1 as the replacement text and click the Replace button.

Source

Stephane Janicaud
  • 3,531
  • 1
  • 12
  • 18
0

I assume you don't have any other capture groups, based on that:

\b(\d+)\/(\1)\b

enter image description here


Regex Demo

Pedro Lobito
  • 94,083
  • 31
  • 258
  • 268