1

I found similar question Regular expression to match sets of numbers that are not equal nor reversed but I'm bad in regexp so need an advice. I have string, i.e: 3344654865 and I need to know that first characters are two not equal pairs. I tried this regexp

^(?:(\d)(?:\1)(\d)(?:\2))

but it captures only first digit in pair

1.  [0-1]   `3`
2.  [2-3]   `4`

In example 3344654865 it should capture two pairs of digit, where digits inside pair are equal 3=3, and 4=4. How to upgrade my regexp that it will return two not equal pairs from the begining of the string ?

Community
  • 1
  • 1
Valery Statichny
  • 593
  • 7
  • 22

3 Answers3

2

this worked for me ^((\d)\2)(?!\1)(\d)\3.*
matches first characters are two not equal pairs
Demo

Update: per request ^((\d)\2)(?!\1)((\d)\4) desired match are no. 1 & 3
Demo

if matches have to be 1 & 2 then use one of previous patterns to verify it meets your criteria then apply this pattern ^(\d\d)(\d\d)

alpha bravo
  • 7,838
  • 1
  • 19
  • 23
1
^((\d)\2)(?!\1)((\d)\4)

Result:

3344654865 -> (33, 3, 44, 4)
12345      -> fail (digits not same in pairs)
9          -> fail (too short)
3333       -> fail (two pairs same)

It is hard to eliminate the intervening groups of one character since they're needed for backreferences.

Amadan
  • 191,408
  • 23
  • 240
  • 301
  • It does work, saying that it is not a match (33 -> fail, 34 -> 34, 345 -> 34), according to your specification ("I need to know that first characters are two not equal pairs"). Otherwise I don't understand your question; please specify the desired input and desired output. – Amadan Jun 03 '14 at 04:30
  • Input: 3344654865 Desired output:3344 Sorry for my bad explanation – Valery Statichny Jun 03 '14 at 04:32
  • What would be the output for input 3737? How about for 12345? What about 9? – Amadan Jun 03 '14 at 04:34
  • In example with 3737 it should be skiped. I try to get pairs where first and second digit equals, i.e 55, 77, etc. – Valery Statichny Jun 03 '14 at 04:38
  • Why not answer the question fully? Is `12345` valid or not? Should the digits in pairs be same (like `33`, `44`) or is it just a coincidence in your case, since it is nowhere listed explicitly, except in your code sample that you state doesn't work? I assume yes, but for the future, you need to write several cases for regexp matches, to cover all the situations, not only the ideal one you are thinking about. – Amadan Jun 03 '14 at 04:42
  • Sorry, 12345 is not valid, yes - digits in pairs should be same. I'm new in regexp, so don't know how to give descriptive examples. – Valery Statichny Jun 03 '14 at 04:45
  • I don't understand, I posted the same exact pattern 19 minutes ago and did not get the check mark !!!?? – alpha bravo Jun 03 '14 at 04:52
  • @alphabravo: Yeah, it does appear your update is the same as my code. Which means you must be a mind reader! :) (since I was at that time still trying to get the correct requirements.) However, I'm not, so can't answer why OP accepted mine and not yours. The best I can do is just give you a +1 for a correct answer. – Amadan Jun 03 '14 at 05:17
1

This:

^(\d+?)(?!\1)(\d+)

will give you:

 33 - 44654865

it is not trivial to make the second part of the same length, but the first digits of the second part are guaranteed to be not the same as the first one. So you will have to get them yourself.

By not trivial, I mean you need to try 1 digit, 2 digits, 3 digits, and so on with alternation or a loop.

perreal
  • 94,503
  • 21
  • 155
  • 181