Regular expressions

0

I'm working on an interpreter with regular expressions, right now I'm stuck, the problem is that I want to remember a pattern more than 1 time:

^(${name})${descrip.rules.skip}+(${descrip.rules.assig})${descrip.rules.skip}*(${name}|${type.decimal}|${type.num}|${type.vof}|${type.texto})${descrip.rules.skip}*(?:(\+|-|\/|%)${descrip.rules.skip}*(${name}|${type.decimal}|${type.num}|${type.vof}|${type.texto}))*${descrip.rules.skip}*!$

At the end I use 3 parentheses:

  • is all the pattern I want to remember but I put it on?: so that be like this
  • the pure operator
  • the value
  • Now I want to remember an operation like this: 5+4/2

    But what reminds me is only: 5/2

    Any ideas on how to make me remember the rest?

        
    asked by Salvador Miron Ramos 11.10.2018 в 06:52
    source

    1 answer

    1

    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);
        
    answered by 18.10.2018 в 17:48