Author Topic: .NET Regex Split ; except within () [] {}s  (Read 737 times)

0 Members and 1 Guest are viewing this topic.

Offline DorkeyDear

  • Veteran
  • *****
  • Posts: 1507
  • I also go by Curt or menturi
.NET Regex Split ; except within () [] {}s
« on: November 16, 2008, 09:08:29 pm »
I was wondering if anybody could help out. I'm creating my own for-fun programming language in C# using the .NET framework and am doing fairly well considering I'm pretty new at this, and was wondering if anybody knew how to how to split a string by the ; character, except when its within (), {}, or []s. So basically:

"monkey;\n
apple = ${\n
  food;\n
  two;\n
};\n
apply;"
would become
"monkey;"
"\napple = ${\n
  food;\n
  two;\n
};"
"\napply;"

ATM, I am just using this:
List<string> lines = new List<string>(new Regex(";").Split(code));
which splits by ";", anywhere though


I forgot to say that the {}, (), and []s can be nested...
« Last Edit: November 16, 2008, 09:15:38 pm by DorkeyDear »

Offline Brock

  • Flagrunner
  • ****
  • Posts: 605
  • [GG]StrykeR
    • Gunner Guards Clansite
Re: .NET Regex Split ; except within () [] {}s
« Reply #1 on: November 16, 2008, 11:09:08 pm »
You do that for FUN?  >_>
"What is the Matrix? Control. The Matrix is a
computer-generated dream world built to keep us
under control in order to - wait, what?"

Offline bja888

  • Flagrunner
  • ****
  • Posts: 745
  • Working
    • Bja888.com
Re: .NET Regex Split ; except within () [] {}s
« Reply #2 on: November 17, 2008, 06:03:37 pm »
Post the input and the output plz. I'll give you the code that makes it work.

Offline DorkeyDear

  • Veteran
  • *****
  • Posts: 1507
  • I also go by Curt or menturi
Re: .NET Regex Split ; except within () [] {}s
« Reply #3 on: November 17, 2008, 07:52:55 pm »
Input:
Code: [Select]
one;\n
two = ${\n
  three;\n
  four = {\n
    five;\n
    return six;\n
  };\n
  return seven;\n
};\n
eight;\n
nine = ${\n
  return ten;\n
};\n
eleven;
output: (a string[] or List<string>)
Code: [Select]
one;
Code: [Select]
\n
two = ${\n
  three;\n
  four = {\n
    five;\n
    return six;\n
  };\n
  return seven;\n
};
Code: [Select]
\n
eight;
Code: [Select]
\n
nine = ${\n
  return ten;\n
};
Code: [Select]
\n
eleven;

the \n means line break here

and @Brock - Yes, among other things as well...

Offline bja888

  • Flagrunner
  • ****
  • Posts: 745
  • Working
    • Bja888.com
Re: .NET Regex Split ; except within () [] {}s
« Reply #4 on: November 18, 2008, 07:09:40 am »
This is close, but not quite there.

Code: [Select]
\w+\s*(=[^}]+})?;

Offline bja888

  • Flagrunner
  • ****
  • Posts: 745
  • Working
    • Bja888.com
Re: .NET Regex Split ; except within () [] {}s
« Reply #5 on: November 18, 2008, 06:15:07 pm »
You would be better crawling it. Would you like pseudo code or real code for that?