Problem with String.Split

2

My problem is this: I want to get different objects from a string , dividing them by the character ; , except those in parentheses.

Example:

Linha1;
Linha2;
L i n h a 3;
(linha que não quero que apareça; outra; mais uma;)
Linha 4;

And so it's coming out in array :

Lista1, Linha2, L i n h a 3, linha que não quero que apareça, outra, mais uma, linha 4

But I do not want the line that I do not want to appear, either the "other" or the "plus one" is included in this array , or summarizing: I do not want anything in parentheses to be included in the list.

Update

No one can understand what I want, here's an example of my code in my language:

{
  echo "Olá, mundo!";
  echo "Aqui é outro bloco!";
  def MeuTeste = "24055";
  if 123 = 123 (echo "Executa isso!"; echo "isso também!";);
  if 123 = 545 (echo "Não execute isso!"; echo "ignore isso!";);
  pause;
}

Here is the structure that separates each statement with the character ; :

For currentStatement As Integer = 0 To Statements.Split(Separator).Length - 1
     Dim currentIndex As String() = Statements.Split(";"c)
     Dim currentText As String = currentIndex(currentStatement) 
     .... comandos que executam o currentText

Correct was to return the values of this variable currentIndex :

echo "Olá, mundo!"
echo "Aqui é outro bloco!"
def MeuTeste = "24055"
if 123 = 123 (echo "Executa isso!"; echo "isso também!";)
if 123 = 545 (echo "Não execute isso!"; echo "ignore isso!";)
pause

But here's the problem, and it returns like this:

echo "Olá, mundo!"
echo "Aqui é outro bloco!"
def MeuTeste = "24055"
if 123 = 123 (echo "Executa isso!"
echo "isso também!"
)
if 123 = 545 (echo "Não execute isso!"
echo "ignore isso!"
)
pause

Moral of the story, it separates everything that is between parentheses and my question is this: How do I not separate what's inside the parentheses?, anyway, I do not want which excludes what is in the parentheses, but just do not separate each thing by the character ; .

    
asked by anonymous 07.06.2015 / 02:23

2 answers

1

You can use the expression ;(?!.*\)|\() to split the text using as the ; delimiter, whichever is between parentheses will be ignored.

public static void Main() {
    string codigo = 
    @"echo ""Olá, mundo!"";
echo ""Aqui é outro bloco!"";
def MeuTeste = ""24055"";
if 123 = 123 (echo ""Executa isso!""; echo ""isso também!"";);
if 123 = 545 (echo ""Não execute isso!""; echo ""ignore isso!"";);
pause;";

     var linhas = Regex.Split(codigo, @";(?!.*\)|\()");
     foreach (var linha in linhas) {
          Console.Write(linha);
          // echo "Olá, mundo!"
          // echo "Aqui é outro bloco!"
          // def MeuTeste = "24055"
          // if 123 = 123 (echo "Executa isso!"; echo "isso também!";)
          // if 123 = 545 (echo "Não execute isso!"; echo "ignore isso!";)
          // pause
     }
     Console.ReadLine();
}

View demonstração

    
07.06.2015 / 23:02
3

The String.split method accepts a expressão regular not just one character.

Moral of the story, you need a regular expression that ignores characters within ()

The expression will be this:

(; | \ (. * \))

Try this:

public static void main(String[] args) {
    String str = "Linha1;Linha2;L i n h a 3;(linha que não quero que apareça; outra; mais uma;)Linha 4;";

    String[] parts = str.split("(;|\(.*\))");

    System.out.println(Arrays.toString(parts));
}

Is not that it yet?

then use this site and build your regular expressions:

link

For .Net platform in the Visual Basic language, the logic is the same and the regular expression, tb, however, consider this reference link to implement:

link

    
07.06.2015 / 02:45