I have some problem to write the regular expression for if-then-else statement. For example I have the following string (all the char are on the same row):
if(%codizione1%) then istruzione1; istruzione1.2; else if(%condizione2%) then istruzione2; istruzione 2.1; istruzione 2.2; else if(%condizione3%) then istruzione3; end if; end if; end if;
and I tryed to write this regex but it didn't work:
if\\s?\\(\\%(.+?)\\%\\)\\s?then (.*?) ((?:else?))*(.*)end if;
In the example I want to capture: group(0): all the string; group(1): the condition (%codizione1%) group(2): the instruction between the then and the else (istruzione1; istruzione1.2;) group(3): the rest ot the instruction after group(2)(else if(%condizione2%) then istruzione2; istruzione 2.1; istruzione 2.2; else if(%condizione3%) then istruzione3; end if; end if;)
The else must be optional, I can have something like:
if(%a=b%) then c=d; end if;
Please help me. Thank you guys.
"if\\s?..."or more like"if\\\\s?..."? And why don't you use a real parser instead? Programming languages normally aren't regular so regular expressions are a poor fit.