To explain myself I will do it with a simpler example.
The problem is that you are crushing the capture groups with subsequent searches.
For example, consider the following input 5-4-2
and the regular expression: (\d)(?:(-)(\d))+
that would only search for numbers and the subtraction sign (but that works in the same way as your expression)
- The first group is the first number
- The second group is the operation (to simplify, always subtract)
- The third group is the next number
The problem is that with this expression: (?:(-)(\d))+
captures -4
, but as you can repeat for the +
, then you capture -2
and crush the values you had
in the second and third capture group. You always keep the last.
Thus, after executing it, the first group would have 5
, the second -
and the third 2
Example
A possible alternative would be to capture the complete set of operations and then go searching with a loop. For example, for the simplified example:
We would look for: (\d)((?:-\d)+)
Now the second group has -4-2
, which we will store in a variable. Now we will carry out a new search in this new variable for (-)(\d)
(with global flag g
) and for each iteration we will save group 1 and 2 in some variable (as an array)
Here is the whole process (simplified) with javascript:
let match = '5-4-2'.match(/(\d)((?:-\d)+)/);
let primero = match[1];
let segundo = match[2];
console.log('La segunda parte es: ' + segundo + ', Vamos a procesarla');
let operaciones = [];
let index_operaciones = 0;
const regexsegundo = /(-)(\d)/g;
while ((m = regexsegundo.exec(segundo)) !== null) {
// Para evitar bucles infinitos con búsquedas de longitud 0
// Si no es tu caso, puedes quitar este IF
if (m.index === regexsegundo.lastIndex) {
regexsegundo.lastIndex++;
}
operaciones[index_operaciones] = [];
operaciones[index_operaciones][0] = m[1];
operaciones[index_operaciones][1] = m[2];
index_operaciones++
}
console.log('Resultados_________________');
console.log('Primero = ' + primero);
console.log('Luego = ');
console.log(operaciones);