How to get specific text from a string with Regex?

-1

I'm trying to manipulate the return of a string where I need to always get the where block from the select problem so my method always returns different strings.

For example:

SELECT.....
FROM.....   
WHERE
    (
        ( 
        UPPER(INDICADO.IND_DESCRI) IS NULL AND
        UPPER(CASE WHEN IND_PERIOD = 0 THEN 'LABELDIARIA'  WHEN IND_PERIOD = 1 THEN 'LABELSEMANAL'  WHEN IND_PERIOD = 2 THEN 'LABELQUINZENAL'  WHEN IND_PERIOD = 3 THEN 'LABELMENSAL'  WHEN IND_PERIOD = 4 THEN 'LABELBIMESTRAL'  WHEN IND_PERIOD = 5 THEN 'LABELTRIMESTRAL'  WHEN IND_PERIOD = 6 THEN 'LABELSEMESTRAL'  WHEN IND_PERIOD = 7 THEN 'LABELANUAL' END) = @PERIODICIDADE1 
        ) 

    )
    Order by ......

You would need to get in a variable only with the where.

    
asked by anonymous 05.02.2016 / 11:52

1 answer

2

No C # handler, but I made a javascript code using Regex that can bring you some idea. If you have any other implementations, let's say we're implementing this code:

(I edited and made it simpler)

function PegaWHERE( query ) {
  
  query = query.replace(/\n/g, '%\n%'); // Remove quebras de linha
  
  var match = /(WHERE.*)ORDER/i.exec( query ); // Procura bloco WHERE quando ORDER existe
  
  if ( ! match ) { // Caso ORDER não exista)
  
    var match = /(WHERE.*)$/i.exec( query ); // Procura bloco WHERE sem ORDER
  
  }
  
  if ( ! match ) { // Caso WHERE não exista
    
    //return '';
    alert(''); return;
  }
  
  var where = match[1].replace(/%\n%/g, "\n").trim(); // Devolve quebras de linhas

  //return where;
  alert( where );
}
<textarea id="query" style="width:400px;height:100px;">select * from usuario 
where cod_usuario = 1
order by usu_nome</textarea>

<br>

<button onclick="PegaWHERE( document.getElementById('query').value )" >PEGA WHERE</button>
    
05.02.2016 / 18:00