How to make the Split in some lines except specific?

Asked

Viewed 294 times

1

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

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 lines separated by new lines, for example what I want to:

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 this is returning:

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:

inserir a descrição da imagem aqui

This is the Regex I’m wearing: (?!.*('|\(|\)))\n

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

  • A hint would be to remove comments that are not suitable for execution. Replace (#.)(\n) by "", then you pass your regex. What do you think?

  • 1

    From what I understand by looking at your example of what’s right, you’re looking to make a parser. It’s something far more complex than you think and I think it’s unlikely that someone will give you some just with this reward. And if I understood his ultimate goal, the parser is even better because you will have a lot of problems of this type. The description of the problem does not match much with the example.

  • 1

    has to post part of the code to understand better?

1 answer

1


I know it’s not what you want but the best answer I can give you is not to use RegEx for this. Not least because that’s not all you’ll want to do with this string. His example already shows why. I even thought of making a pattern that would suit him but he would be bored, pure waste of time.

I appreciate your willingness to do this, it’s better than most programmers who just follow cake recipe and don’t even understand how things work. You want to make a programming language. But you have to do it the right way.

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

I strongly recommend starting reading on the subject of Parsing or the creation of programming languages. While it is technically possible to create a set of standards to accomplish the task, it will become so complex, inefficient and probably problematic that it is best to simply forget RegEx.

I don’t put code because it depends on a number of factors and it’s something too big to be an answer.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.