How to get specific string text with Regex?

Asked

Viewed 259 times

-1

I’m trying to manipulate the return of a string where I always need to get the block Where do select the problem is that 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 ......

Would need to get in a variable only with the Where.

  • 2

    I couldn’t understand your problem. Try to be a little more specific...

  • You’ll always have a order by afterward?

  • Not only ADO.NET

  • Yes can so I need something dynamic because I do not know what can contain later.

  • You’d have to have everything possible statements that may come after a where and validate for it...

  • You’ll just have to copy the string from Where until you find something that might look like GROUP BY, ORDER BY, etc. or the end of the string, whichever comes first.

  • You can use REGEX but you will always need to have Where on a single separate line. https://regex101.com/r/nA3kO7/1

  • Can there be more than one query in the same code? That is (they are usually separated by a comma point, as in the example) select.....; select...

  • @Khaosdoctor worked his example most when it has line break or "(" does not catch.

  • Precisely, in the case of this example the Where query has to be inline and only have it on the line. If we didn’t have to take other examples to be able to develop a more complex regex

  • @Khaosdoctor So I thought I’d take everything including the Where until the order by is possible?

Show 6 more comments

1 answer

2

I don’t know C#, but I made a javascript code using Regex that might give you some idea. If you have any more implementation, say we will implement 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>

  • This way it works Where[ s S](?=order)|Where[ s S](?=group)|Where[ s S]*

  • I edited the code, made it simpler, but basic. It finds the WHERE block if ORDER exists or not, and if by chance WHERE also does not exist, it returns an empty string. This is all independent whether or not the query has line breaks.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.