简体   繁体   English

除了某些字符组合之外的Split()字符串

[英]Split() string except for certain character combination

I want something like: 我想要的东西:

"aaaXaaaXaaaXaaaYXaaa".Split('X');

but want it to ignore 'YX'. 但是希望它忽略'YX'。

Of course I can simply loop and correct for it. 当然,我可以简单地循环并纠正它。 But is there a built-in method for that? 但是有没有内置的方法呢?

You can use a regular expression with a negative lookbehind: 您可以使用带有负向lookbehind的正则表达式:

string[] result = Regex.Split(s, "(?<!Y)X");

See it working online: ideone 看到它在线工作: ideone

More information about lookarounds: Lookahead and Lookbehind Zero-Width Assertions 有关外观的更多信息: Lookahead和Lookbehind Zero-Width Assertions

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

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