Hack Forums
[C#] Regex problem. - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [C#] Regex problem. (/showthread.php?tid=5598136)



Regex problem. - i3urn - Apr 12, 2017

Trying to create a regex that grabs the conditions and statements from if statements.

Currently I have this.
Code
if\((.*)\)[\r\n]*{[\r\n]*(.*)[\r\n]*}

But it only matches single line statements such as
Code
if(value >= 5)
{
DoSomething();
}

When I need it to match multi-line statements such as
Code
if(value >= 5)
{
DoSomething();
DoSomethingElse();
}

Been testing here any help would be much appreciated :)


* - MasterCurser - Apr 12, 2017

This should work

Code
if\((.*)\)\n{([\r\n]*(.*)[\r\n])*}

Idk why you have [\r\n]* instead of just \n after if() pattern.


* - i3urn - Apr 12, 2017

(Apr 12, 2017 09:31 AM)MasterCurser Wrote:  This should work

Code
if\((.*)\)\n{([\r\n]*(.*)[\r\n])*}

Idk why you have [\r\n]* instead of just \n after if() pattern.

Cheers, that worked perfectly (with a little bit of editing to make it non-greedy)

Code
if\((.*)\)[\r\n]*{([\r\n]*(.*)[\r\n])*?}



* - MasterCurser - Apr 12, 2017

(Apr 12, 2017 09:35 AM)i3urn Wrote:  Cheers, that worked perfectly (with a little bit of editing to make it non-greedy)

Code
if\((.*)\)[\r\n]*{([\r\n]*(.*)[\r\n])*?}

Looks good. 🤝


* - Owl - Apr 12, 2017

Don't lexers omit whitespace and line breaks when parsing?