How to do Split in some specific excepting lines?

1

I want to make a dynamic list that uses Regex.Split separating each item by the new line character, except when in the (...) fields in my lines of code, as follows:

var x = 'the quick fox jumps over the lazy dog'
var n = 'myChar (string:substring, x, 3) !!!'

writeLn 'first value = $x, final = $n'

if $n = $null (
      #aqui está o erro
      #ele da split aqui
      writeLn 'error'
)
#e assim por diante...

I want all rows separated by new rows, example I want :

ind.    valor do elemento
------- -------------------------------------------------
0       var x = 'the quick fox jumps over the lazy dog'
1       var n = 'myChar (string:substring, x, 3) !!!'
2       writeLn 'first value = $x, final = $n'
3       if $n = $null (
            #aqui está o erro
            #ele da split aqui
            writeLn 'error'
        )
4       #e assim por diante...

But you're returning this:

ind.    valor do elemento
------- -------------------------------------------------
0       var x = 'the quick fox jumps over the lazy dog'
1       var n = 'myChar (string:substring, x, 3) !!!'
2       writeLn 'first value = $x, final = $n'
3       if $n = $null (
4           #aqui está o erro    
5           #ele da split aqui
6           writeLn 'error'
7       )
        #e assim por diante...

Below is an image to better describe what is detected:

This is the Regex I am using: (?!.*('|\(|\)))\n

  

I accept responses in Regex, or VB.NET / C # (VB preference)

    
asked by anonymous 29.07.2015 / 00:15

1 answer

1

I know it's not what you want but the best answer I can give you is to not use RegEx for this. Also because this is not all you will want to do with this string . Your example already shows why. I even thought of making a pattern that would suit him but he would be stuck, sheer waste of time.

I appreciate your willingness to do this, it's better than most programmers who just follow cake recipe and do not even understand how things work. You are wanting to do a programming language. But you have to do it right.

Without a parser you will not get any results or even minimally satisfactory. There are ambiguities in the code. In your example if I check the parentheses, it will pick up anything, including existing parentheses within strings , as is the case seen in your text example or still within comments or even consider parentheses in expressions or other context where they can be used. Without having an understanding of what each token of the code is, you can not select anything correctly and perform specific actions. The correct understanding of the context where token is found can only be obtained as a more sophisticated parser mechanism.

I strongly recommend you start reading about parsing or creating programming languages. Although it is technically possible to create a set of standards to accomplish the task, it will be so complex, inefficient and probably problematic that it is best to simply forget RegEx .

I do not put code because it depends on a number of factors and is too big to be an answer.

07.08.2015 / 22:43