Regex to match 2 digit but different numbers

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


Regex to match 2 digit but different numbers



I'm working regex in recent days and now need to make regex which is match with 2 digit but the digits should be different each other
For example followings will be matched:
56, 78, 20 ...
But followings should not be matched:
22, 33, 66 or 99



Already wasted few days for this solution. So any suggestion will be welcome.




1 Answer
1



Capture the first digit, then use negative lookahead with a backreference to that first digit to ensure it isn't repeated:


(d)(?!1)d



https://regex101.com/r/AxH6s8/1



If you need a named group instead:


(?<first>d)(?!k<first>)d



For a general solution of n digits in a row without any repeated digits, you can do something similar, except put d* inside the negative lookahead, before the backreference:


n


d*


^(?:(d)(?!d*g{-1}))+$



https://regex101.com/r/AxH6s8/2





good work. but would you make an example with named group? because my regex is so complex and I can't index them as "1" or "2" simply.
– rener172846
8 hours ago





Not sure what language you're using, but you can use (?<first>d)(?!k<first>)d
– CertainPerformance
8 hours ago




(?<first>d)(?!k<first>)d





I'm using php, and I can't share full regex here. How about to name the first digit as "FirstDigit" such as following (?'FirstDigit'[0-9])(?!(FirstDigit))[0-9] Of course this isn't work so I need to fix that correctly
– rener172846
8 hours ago




(?'FirstDigit'[0-9])(?!(FirstDigit))[0-9]





@rener172846 Another alternative is to use a relative backreference, e.g. (d)(?!g{-1})d which can save naming things you don't otherwise need to.
– Paul Crovella
8 hours ago


(d)(?!g{-1})d





@CertainPerformance if the length of digit isn't fixable, is that still possible with only regex? For example, the number can be any of 4 - 8 digit, and there shouldn't be any same value. so 1245, 24567, 46735 are all match, but 11456, 145467 or 356787 are not match.
– rener172846
1 hour ago








By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Arduino Mega cannot recieve any sketches, stk500_recv() programmer is not responding

Visual Studio Code: How to configure includePath for better IntelliSense results

C++ virtual function: Base class function is called instead of derived