In regex, when you use a star, can you make multiple stars, in a repeated regex, repeat the same number of times?
Sorry for the long question. An example is probably best.
I am trying to match a matrix as a string:
-
[[]]
-
[[][][]]
-
[[0][1][2]]
-
[[,,][,,][,,]]
-
[[0,1,2][3,4,5]]
While regex rarely looks pretty, this is what I came up with:
\[(\[-?(\d+(\.\d*)?)*(,-?(\d+(\.\d*)?)*)*\])+\]
It fits everyone. HOWEVER, commas must always be repeated n times. If there are 5 commas, then 4 commas, then 6 commas, this is not a valid matrix. Is there a flag or variables that I can use? Or is this outside the scope of regex?
source to share
No, regex cannot do this.
Here is a formal proof that a very similar case with you is impossible. This proves that the language L = {a n b n : n ≥ 0} is not a "regular language", so it cannot be matched against a regular expression. This language is essential a*b*
when two stars repeat the same number of times. Your case also includes a star repeating the same number of times, so this is also not possible.
In your case, the best option is probably to use a regex to match each "row" of the matrix, that is: do a global search \[([^][]*)\]
, for each match capturing group 1 that is the content of the string, then count the number of commas and compare all the counts (or separate comma using the language separator operation if you really want numbers.)
PS: [^][]
is the character class of all characters except open and closed parentheses. The closing parenthesis must be the first char after the ^, this is a special case.
source to share