-3

Maybe somebody could help me, how to create regex, that will find two characters with the same amount one after another :

ex. +++--- : true (3) ex. ++++---- : true (4) ex. ++--- : false (2 - 3)

Thanks in forward

Anton Barinov
  • 183
  • 1
  • 3
  • 13
  • 1
    Possible duplicate of [How can we match a^n b^n with Java regex?](https://stackoverflow.com/questions/3644266/how-can-we-match-an-bn-with-java-regex) – PJProudhon Feb 13 '18 at 12:12
  • 1
    As you can see in the link above, it is in fact possible. But depending on the regex flavour you're using it may not be. JavaScript for example doesn't allow recursive patterns, nor balancing groups, plus having a particular behaviour on nested references, it won't match correctly for the given answer. – PJProudhon Feb 13 '18 at 12:15

1 Answers1

1

I don't think you can match something like that in regex.

A workaround would be to split at boundaries between different characters with this regex:

(?<=(.))(?!\1)(?!$)

and see if the resulting array is:

  • length = 2
  • length of first element = length of second element
Sweeper
  • 213,210
  • 22
  • 193
  • 313