Problem with String.Split

Asked

Viewed 413 times

2

My problem is this: I want to get different objects from one 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 it’s coming out like this array:

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

But I don’t want: line that I don’t want to appear", the "other" or the "one more be included in this array, or summarizing: I don’t want anything in parentheses to be included in the list.

Updating

No one is understanding 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 by 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 that’s the problem, and it comes back this way:

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, he separates everything between parentheses and my question is this: How do I not separate what’s in the parentheses? Anyway, I don’t want to exclude what’s in the parentheses, but just don’t separate each thing by the character ;.

2 answers

3

The method String.split accepts a expressão regular not just a 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));
}

That’s not it yet?

then use this site and build your regular expressions:

http://www.regexr.com/

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

https://msdn.microsoft.com/pt-br/library/8yttk7sy(v=vs.110). aspx? Cs-save-lang=1&Cs-lang=Vb#code-snippet-1

  • In BASIC (Visual Basic) it would be the same expression?

  • 1

    The regular expression is the same, a regular expression is a domain-specific language, which changes to Visual Basic, is that you should use: Regex.Split (String, String); https://msdn.microsoft.com/pt-br/library/8yttk7sy(v=vs.110). aspx? Cs-save-lang=1&Cs-lang=Vb#code-snippet-1 .

  • Philip, I may be wrong but you’re sure you’re wrong regex works? for she is marrying even within parentheses. In the question Cypher preferred the languages C# and Vb. NET, the your code is in Java.

  • What happens is that the Regular Expression is also a Language, but not of programming, and it is taken as a Domain Specific Language. There is a reference in my reply to a code in Visual Basic. Languages are just engines, this Web System has always worked for me. And yes, this regular expression works yes: (;|\(.\)) It is that in Java, we have to put one more bar as Escape character, so in fact the expression is this: (;|(.)) I will update the reply. Thank you.

  • @Filipegonzagamiranda Your regex yet doesn’t work, nor in Java, nor in C#. When referring to someone, use the @ in front.

  • Sorry, I made a mess @qmechanik (;|\(.*\))

  • @qmechanik The Stack overflow engine is erasing the bar.... rsrs - It was good that I learned (;| (.* )) Same Java logic... I used two here, but you will see only one now

  • @Filipegonzagamiranda In Java the expression (;|\(.*\)) works!, however in .NET. =/ maybe they’re Engines different, not sure.

  • @Filipegonzagamiranda If possible make explicit in the answer that expression (;|\(.*\)) does not work in the .NET.

  • Well, I haven’t really tested it yet, I still don’t have the project here with me, I’ll be here today and I’ll pass the answer.

  • Well, since that regex there doesn’t work, as it would be in .NET?

Show 6 more comments

1


You can use the expression ;(?!.*\)|\() to divide the text using as delimiter ;, whatever is in 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();
}

See demonstração

  • It didn’t work for the simple reason: It simply removes everything that’s between (...), I don’t want it removed, just don’t include it in the text... For for example, in my compiler each command is separated by the character ";", in the "if" block it is more or less like this: if expressão = outra expressão (statements; segundo;) and is removing the contents of the IF block if I use the String.Split original, will be separated what is inside the block.

  • I edited my post, I hope you understand what I really want.

  • @Cypherpotato I edited the answer. When I have time, if you want, put in VB. NET.

  • I’ll try here...

  • Thank you beast! That’s right, my heart, you’re God’s man ^-^

  • I found a bug... the plus sign + is removed in the array... this is normal?

  • @Cypherpotato . is not normal no. A regex ;(?!.*\)|\() would not exclude him..

  • Ah found the error, was in the coding, was ANSII :P

  • Eeeeh, I found a bug here... https://dotnetfiddle.net/PHkWo4 it does not support multi-line?

  • 1

    Got it, replace that regex with this: ;(?!((.|\n)*)\)|\() and it worked.

Show 5 more comments

Browser other questions tagged

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