0

我需要用分号 ( ;) 作为分隔符来分隔字符串。括号内的分号应该被忽略。

例子:

string inputString = "(Apple;Mango);(Tiger;Horse);Ant;Frog;";

字符串的输出列表应该是:

(Apple;Mango)
(Tiger;Horse)
Ant
Frog

其他有效的输入字符串可以是:

string inputString = "The fruits are (mango;apple), and they are good"

上面的字符串应该拆分成一个字符串

"The fruits are (mango;apple), and they are good"

string inputString = "The animals in (African (Lion;Elephant) and Asian(Panda; Tiger)) are endangered species; Some plants are endangered too."

上面的字符串应该分成两个字符串,如下所示:

"The animals in (African (Lion;Elephant) and Asian(Panda; Tiger)) are endangered species"
"Some plants are endangered too."

我搜索了很多,但找不到上述场景的答案。

有谁知道如何在不重新发明轮子的情况下实现这一目标?

4

2 回答 2

1

使用与您要保留的内容匹配的正则表达式,而不是分隔符:

string inputString = "(Apple;Mango);(Tiger;Horse);Ant;Frog;";

MatchCollection m = Regex.Matches(inputString, @"\([^;)]*(;[^;)]*)*\)|[^;]+");

foreach (Match x in m){
  Console.WriteLine(x.Value);
}

输出:

(Apple;Mango)
(Tiger;Horse)
Ant
Frog

表达意见:

\(           opening parenthesis
[^;)]*       characters before semicolon
(;[^;)]*)*   optional semicolon and characters after it
\)           closing parenthesis
|            or
[^;]+        text with no semicolon

注意:上面的表达式也接受不带分号的括号中的值,例如(Lark)和多个分号,例如(Lark;Pine;Birch)。它还将跳过空值,例如";;Pine;;;;Birch;;;"将是两个项目,而不是十个项目。

于 2012-09-28T14:28:21.380 回答
0

将带括号的情况与“正常”情况分开处理,以确保在前者中省略分号。

实现此目的的正则表达式(匹配输入中的单个元素)可能如下所示(未经测试):

"\([A-Za-z;]+\)|[A-Za-z]+"
于 2012-09-28T14:30:41.140 回答