0

I want to match strings with two numbers of equal length, like : 42-42, 0-2, 12345-54321.

I don't want to match strings where the two numbers have different lengths, like : 42-1, 000-0000.

The two parts (separated by the hyphen) must have the same length.

I wonder if it is possible to do a regexp like [0-9]{n}-[0-9]{n} with n variable but equal?

If there is no clean way to that in one pattern (I must put that in the pattern attribute of a HTML form input), I will do something like /\d-\d|\d{2}-\d{2}|\d{3}-\d{3}|<etc>/ up to the maximum length (16 in my case).

Toby Speight
  • 27,591
  • 48
  • 66
  • 103
Jyah
  • 25
  • 3
  • So, what is so hard in writing a `/\d{2}-\d{2}/` pattern? Or do you want to re-use a part of a pattern? Like use `\d{2}` once in the pattern, and then refer to it inside this same pattern? And what is the regex flavor, BTW? – Wiktor Stribiżew Apr 21 '16 at 15:20
  • take `n` as input and append it to form a string regex – rock321987 Apr 21 '16 at 15:21
  • How large might `n` realistically be? Could you get away with `/\d-\d|\d{2}-\d{2}|\d{3}-\d{3}|/` up to however many it can be? – James Thorpe Apr 21 '16 at 15:22
  • @Wiktor : I want to write something like `/\d{1}-\d{1}|\d{2}-\d{2}|\d{3}-\d{3}|\d{4}-\d{4}|[...]/` pattern and I wonder if it is possible. I don't want to mach only combinations with a length of 2 but any length equal on both parts. @James : I know I can "bruteforce" it but since it could be 1 to 16 length, it pretty dirty to write a so long regex in my mind ^^ – Jyah Apr 21 '16 at 15:23
  • With [recursive regex](http://www.rexegg.com/regex-recursion.html) you could do like this: [`\b(\d(?:-|(?1))\d)\b`](https://regex101.com/r/jV7cD7/1) – bobble bubble Apr 21 '16 at 17:24

2 Answers2

2

This is not possible with regular expressions, because this is neither a type-3 grammatic (can be done with regular expression) nor a type-2 grammatic (can be done with regular expressions, which support recursion).

The higher grammar levels (type-1 grammatic and type-0 grammatic) can only be parsed using a Turing machine (or something compatible like your programming language).

More about this can be found here: https://en.wikipedia.org/wiki/Chomsky_hierarchy#The_hierarchy

Using a programming language, you need to count the first sequence of digits, check for the minus and then check if the same amount of digits follows. Without the minus symbol, this would be a type-2 grammatic and could be solved using a recursive regular expression (even if the right sequence shall not contain digits), like this: ^(\d(?1)\d)$

So you need to write your own, non-regular-expression check code.

David Gausmann
  • 1,570
  • 16
  • 20
  • 1
    Also [*How can we match a^n b^n with Java regex?*](http://stackoverflow.com/questions/3644266/how-can-we-match-an-bn-with-java-regex). But the question is about JS. – Wiktor Stribiżew Apr 21 '16 at 15:38
0

You should probably split the String around the separator and compare the length of both parts.

The tool of choice in regex to use when specifying "the same thing than before" are back-references, however they reference the matched value rather than the matching pattern : no way of using a back-reference to .{3} to match any 3 characters.

However, if you only need to validate a finite number of lengths, it can be (painfully) done with alternation :

  • \d-\d will match up to 1 character on both sides of the separator
  • \d-\d|\d{2}-\d{2} will match up to 2 characters on both sides of the separator
  • ...
Aaron
  • 24,009
  • 2
  • 33
  • 57
  • I want to put this patern in a html input patern attribute so I can't split before testing it. By the way since I have a limited lenght, I'll write it with the painfull method. I just wondered if there were a cleaner and more readable method to write that. Thx for your answer. – Jyah Apr 21 '16 at 15:29
  • Aha, so it is for JavaScript. Right, you cannot recurse subpatterns in JS regex. Only in PCRE, Boost, Ruby and Python PyPi regex. – Wiktor Stribiżew Apr 21 '16 at 15:36