3
Good morning, you guys.
I’m trying to make a regex to capture blocks of CASE WHEN ... END. So that string:
iduser + CaSe WhEn ("end") = 0 THEN 'CASE WHEN' ELSE ' END ' END + sum(iduser ) + CASE WHEN LANGUAGE = 3 THEN 4 ELSE 5 END
The blocks are captured:
CaSe WhEn ("end") = 0 THEN 'CASE WHEN' ELSE 'END' END
CASE WHEN LANGUAGE = 3 THEN 4 ELSE 5 END
What I get so far, was the result of a regex that I had done with a lot of sweat to catch string between brackets, it’s like this:
(CASE\\s*WHEN)([^)]+)(END)
But there are some problems:
([^)]+)
-> This part doesn’t make sense to me, but without it, no works;- I don’t want regex to capture blocks in quotes (simple or doubles).
([^)]+)
-> Besides not making sense, it makes string with parentheses not work.
EDIT
The regex is like this now, I managed to evolve a little:
(?i)(CASE\s*WHEN)(\s*.*)(END)
Now you no longer have the meaningless parenthesis and you are case insensitive. But, you are still not ignoring the quotation marks. And this modification has stopped taking all the blocks and is bringing them all together into one.
Thanks in advance!
Can be with Java or has to be with regex even?
– igventurelli
It could be Java. But I would love to use regex in this solution.
– Eduardo H. M. Garcia
could post your code with the quotes that are being ignored ?
– Marco Souza
Ex: CASE WHEN LANGUAGE = 0 THEN 'end' ELSE 'case when' END. regex must ignore the terms between single and double quotes.
– Eduardo H. M. Garcia
Edit the question and put the string "true".
– igventurelli
As well string "true"?
– Eduardo H. M. Garcia
Example string has no quotation mark
– igventurelli
Places the case tb variations
– igventurelli
I edited. But this problem is written in the body of the question. It is important to read everything before, to understand the problem as a whole.
– Eduardo H. M. Garcia
There is even a solution with regex, but there are 2 points: it uses features that Java does not support, and I think it is not the right tool for this problem. Maybe the solution is to use something like antlr, for example
– hkotsubo