正则高手进,问一个关于字符串 分割的问题
字符串“(AAA)\(BBB).(CCC)-(DDD) (FFF) -(GGG)-H1-(H2-H3)”想要得到下面结果就是 (内容和分隔符)AAA
\
BBB
.
CCC
-
DDD
(空格)
FFF
(空格)-
GGG
-
H2-H3
----------------
不取H1,因为没有在括号中()
--------------------编程问答--------------------
string str = @"(AAA)\(BBB).(CCC)-(DDD) (FFF) -(GGG)-H1-(H2-H3)";
Match m = Regex.Match(str, @"\((?<p>[^\)]+)\)(?<c>.{1})?");
while (m.Success)
{
Response.Write(m.Groups["p"].Value + "\r\n");
Response.Write(m.Groups["c"].Value + "\r\n");
m = m.NextMatch();
}
我测试是可以的。。应该满足需求 --------------------编程问答--------------------
谢谢这位兄弟可是,不灵啊,两个字符为分割符的怎么处理,3个的呢? --------------------编程问答-------------------- Regex.Split(@"(\([^)]+\))") --------------------编程问答--------------------
string str = @"(AAA)\(BBB).(CCC)-(DDD) (FFF) -(GGG)-H1-(H2-H3)";
Match m = Regex.Match(str, @"\((?<p>[^\)]+)\)(?<c>[^\W]+)?");
while (m.Success)
{
Response.Write(m.Groups["p"].Value + "\r\n");
Response.Write(m.Groups["c"].Value + "\r\n");
m = m.NextMatch();
} --------------------编程问答--------------------
string str = @"(AAA)\(BBB).(CCC)-(DDD) (FFF) -(GGG)-H1-(H2-H3)";
Match m = Regex.Match(str, @"\((?<p>[^\)]+)\)(?<c>.{1})?");
while (m.Success)
{
Response.Write(m.Groups["p"].Value + "\r\n");
Response.Write(m.Groups["c"].Value + "\r\n");
m = m.NextMatch();
}
我测试是可以的。。应该满足需求
谢谢这位兄弟可是,不灵啊,两个字符为分割符的怎么处理,3个的呢?
string str = @"(AAA)\(BBB).(CCC)-(DDD) (FFF) -(GGG)-H1-(H2-H3)";
Match m = Regex.Match(str, @"\((?<p>[^\)]+)\)(?<c>[^\W]+)?");
while (m.Success)
{
Response.Write(m.Groups["p"].Value + "\r\n");
Response.Write(m.Groups["c"].Value + "\r\n");
m = m.NextMatch();
}
这样搞出来分隔符都变成空了 --------------------编程问答--------------------
Regex.Split(@"(\([^)]+\))")
这样的话 -H1- 成分隔符了!
分割符只能是特殊字符! --------------------编程问答-------------------- 你不会在分割后,再处理一下数组内奇数位的元素么? --------------------编程问答-------------------- string str = @"(AAA)\(BBB).(CCC)-(DDD) (FFF) -(GGG)-H1-(H2-H3)";
var ary = Regex.Split(str, @"(?is)(?:\(|\)(?=[^()a-z0-9]+)|\)[^()]+?[a-zA-Z0-9][^()]*)");
--------------------编程问答--------------------
string str = @"(AAA)\(BBB).(CCC)-(DDD) (FFF) -(GGG)-H1-(H2-H3)";
var ary = Regex.Matches(str, @"\((?<k>[^()]+)\)|(?<w>(?<=\))[^()a-z0-9A-Z]+(?=\())").OfType<Match>().Select(t => t.Groups["k"].Value + t.Groups["w"].Value).ToArray();
补充:.NET技术 , ASP.NET