简体   繁体   English

将字符串拆分为以前缀开头的子字符串

[英]split string into substrings that start with prefixes

I'm looking to split a string into substrings and each of the substrings starts and ends with [[ ]] 我正在将字符串拆分为子字符串,每个子字符串以[[]]开始和结束

eg 例如

I've only used a dash and colon as examples - it could be anything including space or no space. 我仅以破折号和冒号为例-可能是任何东西,包括空格或没有空格。 I'm just looking to get each of the substrings including the square brackets. 我只是想获取包括方括号在内的每个子字符串。

string mystring = "[[string 1]] - [[string 2]] : [[string 3]]"

Can I split this into 我可以把它分成

[[string 1]]
[[string 2]]
[[string 3]]

A list will do... 列表会...

Any ideas? 有任何想法吗?

Thanks, 谢谢,

string input = "[[string 1]] - [[string 2]] : [[string 3]]";
var parts = Regex.Matches(input, @"\[\[.+?\]\]").Cast<Match>().Select(x => x.Value)
                 .ToArray();
string input = "[[string 1]][[string 2]][[string 3]]";
input = input.Replace("][","]-[");
List<string> result = input.split('-').ToList();

If you want an array, replace the last line with: 如果需要数组,请用以下命令替换最后一行:

string[] result = input.split('-');

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

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