简体   繁体   English

条件分裂

[英]Conditional splitting

I am writing a .cpp parser in C#.我正在用 C# 编写一个 .cpp 解析器。 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:在 C# String.Split就足够了:

  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");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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