6

I'm using this regex ^x{3}y{3}$ to check for example this string: xxxyyy, where the number of x's is the same as the number of y's (in my example it is 3). But the problem is that I don't know the number of repetitions (it is in range from 0 to 10), so 3 has to be replaced with something abstract.

I can use ^x*y*$ or ^x{0,10}y{0,10}$, but this will not work when the number of x's is different from the number of y's.

I'm looking for something like \1, which is used for reusing of matched group, but in my case I want to reuse the number of group matches for x.

Termininja
  • 6,620
  • 12
  • 48
  • 49
  • Not possible in regex. You need the help of an external language for that. For the sake of completeness, say what regex engine you are using. (I think this is a sub-set of the palindrome problem, with cannot be solved with regex, either. See http://stackoverflow.com/questions/233243/how-to-check-that-a-string-is-a-palindrome-using-regular-expressions) – Tomalak Aug 28 '16 at 10:08
  • @Tomalak He gave the link: https://regex101.com/r/rP7wB8/2 So I think he is using PCRE? – Nehal J Wani Aug 28 '16 at 10:12
  • @Nehal I don't know. That link is not enough to draw a conclusion from. – Tomalak Aug 28 '16 at 10:14
  • 5
    For PCRE is is possible, see https://regex101.com/r/lJ6gH1/1 – Sebastian Proske Aug 28 '16 at 10:15
  • I'm using http://robotframework.org/ – Termininja Aug 28 '16 at 10:16
  • @SebastianProske 's answer nails it! Wow. – Nehal J Wani Aug 28 '16 at 10:17
  • @Nehal Unfortunately though, the OP is using Python. – Tomalak Aug 28 '16 at 10:22
  • 1
    Python alternate regex module supports recursions. @Tomalak – revo Aug 28 '16 at 10:23
  • 1
    @revo Doubtful that a framework like the one the OP uses allows changing that. – Tomalak Aug 28 '16 at 10:25
  • Can you show how you are using it? Are you calling a keyword? Is this being used inside a python-based keyword? Within the test,do you know what the number is, or is the problem that you never know the number, and instead the pattern must be "find a number of y's that matches the length of the preceding x's". – Bryan Oakley Aug 28 '16 at 13:14
  • @BryanOakley, how I already wrote, I have to check the string, but I don't know the number of repetitions, it depends. It is an Testing Framework which is extension of the Robot Framework. I put my test data in Excel file, where I can use Python code in some cell to check some value or compare some values. This is why I need inline and very short code. But my question is more abstract, because I'm interested to know how to do such thing in other cases when needed and for different programming languages. – Termininja Aug 28 '16 at 13:25
  • What I'm getting at, is whether you can use a variable to define the value `3`. What keyword are you calling? Are you passing in a regular expression? Can you embed a variable in the regular expression? eg: `^x{${number}}y{${number}}`? – Bryan Oakley Aug 28 '16 at 13:29

1 Answers1

3

Since you've got a fixed upper limit, you could just list the matches out longhand:

^(x{10}y{10}|x{9}y{9}|x{8}y{8}|x{7}y{7}|x{6}y{6}|x{5}y{5}|x{4}y{4}|x{3}y{3}|x{2}y{2}|xy)?$

(The ? handles your "zero of each" case.)

Clearly if x and y are long in practice then there will be lots of repetition here.

Another approach would be to repeatedly replace xy with an empty string, and check that you end up with an empty string (since each time you remove a pair, you'll bring another pair together).

Tomalak
  • 332,285
  • 67
  • 532
  • 628
Matthew Strawbridge
  • 19,940
  • 10
  • 72
  • 93