简体   繁体   中英

Conditional splitting

I am writing a .cpp parser in C#. I need to split the file by some operators. However, I have two delimiters, - and -> .

I want to split the file by > when it doesn't have a - preceding, otherwise > delimiter would also split the -> .

Should I use regex, or any different solutions?

In C# String.Split is enough:

  String source = "1->2>3->4->5>6"; 

  // "1", "2", "3", "4", "5", "6"
  var items = source.Split(new String[] { "->", ">" }, StringSplitOptions.None);

Just show how to do this use Regex:

String source = "1->2>3->4->5>6";
//replace all > to ->
source=Regex.Replace(source,"(?<!-)>","-$0");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM