0

The title may not best describe what I'm trying to do (couldn't think of a better way to word it nicely).

I'm looking to use the exec() function to capture groups where the count for one character is equal to the count of another. For example, using characters x and y:

xx&yy [match: x count = 2, y count = 2]
xx&yx&&yy [match: x count = 3, y count = 3] 
xyxx&&&y [match: x count = 1, y count = 1] [no match: x count = 2, y count = 1]

Is this possible to do?

sookie
  • 1,989
  • 3
  • 21
  • 37
  • That's not a good choice of dupe. Brackets can be matched with recursion, but even recursion wouldn't be enough to solve this specific problem. _However_, a pattern with a certain number of hard-coded xs and ys may be good enough for practical purposes, for example: `x(?:[^xy]*x[^xy]*y)*[^xy]*y`. – Aran-Fey Apr 19 '17 at 23:28
  • @Rawing The problem is equivalent to matching balanced brackets, just set `x` to `(` and `y` to `)`. – kennytm Apr 19 '17 at 23:31
  • @kennytm I don't think the x and y need to be balanced. OP says the text needs to start with an x, but other than that I can't see any restrictions on the order of the x and y characters. – Aran-Fey Apr 19 '17 at 23:35
  • @Rawing Having "count of `x` = count of `y`" is equivalent to balancing. – kennytm Apr 19 '17 at 23:38

0 Answers0