Regex: match a non nested code block
Posted
by
Sylvanas Garde
on Stack Overflow
See other posts from Stack Overflow
or by Sylvanas Garde
Published on 2014-05-31T00:34:34Z
Indexed on
2014/05/31
3:26 UTC
Read the original article
Hit count: 241
I am currently writing a small texteditor. With this texteditor users are able to create small scripts for a very simple scripting engine.
For a better overview I want to highlight codeblocks with the same command like GoTo(x,y) or Draw(x,y).
To achieve this I want to use Regular Expresions (I am already using it to highlight other things like variables)
Here is my Expression (I know it's very ugly):
/(?<!GoTo|Draw|Example)(^(?:GoTo|Draw|Example)\(.+\)*?$)+(?!GoTo|Draw|Example)/gm
It matches the following:
lala
GoTo(5656) -> MATCH 1
sdsd
GoTo(sdsd) --comment -> MATCH 2
GoTo(23329); -> MATCH 3
Test()
GoTo(12) -> MATCH 4
LALA
Draw(23) -> MATCH 5
Draw(24) -> MATCH 6
Draw(25) -> MATCH 7
But what I want to achieve is, that the complete "blocks" of the same command are matched. In this case Match 2 & 4 and Match 5 & 6 & 7 should be one match. Tested with http://regex101.com/, the programming lanuage is vb.net. Any advise would be very useful, Thanks in advance!
© Stack Overflow or respective owner