我有一个简单的字符串:
data1:abc,123,xyz,data2:hello,goodbye我需要regex返回一个匹配的集合:
abc
123
xyz在过去,我会使用正则表达式:
data1:(.*)data2:然后在逗号上拆分它的输出。
有没有一种方法可以作为一个正则表达式而不需要外部代码来实现?
发布于 2012-03-14 23:33:23
尝尝这个
String text = "data1:abc,123,xyz,data2:hello,goodbye";
Regex reg = new Regex(@"(?<=data1:.*)[^,]+(?=.*data2)");
MatchCollection result = reg.Matches(text);
foreach (var item in result) {
Console.WriteLine(item.ToString());
}输出:
abc
123
xyz
发布于 2012-03-14 23:11:09
不确定,但你可以这样做
String s="data1:abc,123,xyz,data2:hello,goodbye "
sttring[] slst= s.split(":");
for (int i = 0;i<slst.lemgth;i++)
{
string[] inr = slst[i].split(",");
for (int j = 0;j<inr.lemgth;j++)
{
if((inr.IndexOf("data") != -1)
continue;
//your code
}
} https://stackoverflow.com/questions/9704515
复制相似问题