我拥有什么
string ImageRegPattern = @"http://[\w\.\/]*\.jpg|http://[\w\.\/]*\.png|http://[\w\.\/]*\.gif";
string a ="http://www.dsa.com/asd/jpg/good.jpgThis is a good dayhttp://www.a.com/b.pngWe are the Best friendshttp://www.c.com";
我想要什么
string[] s;
s[0] = "http://www.dsa.com/asd/jpg/good.jpg";
s[1] = "This is a good day";
s[2] = "http://www.a.com/b.png";
s[3] = "We are the Best friendshttp://www.c.com";
提示:
如果URL可以像下面这样分割,那就更好了,但如果不行也没关系。
s[3] = "We are the Best friends";
s[4] = "http://www.c.com";
问题是什么
我尝试使用下面的代码来分割字符串,
string[] s= Regex.Split(sourceString, ImageRegPattern, RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace);
但结果不太好,似乎Split方法取出了所有与ImageRegPattern匹配的字符串。但是我希望它们留下来。我在MSDN的RegEx页面上检查了一下,似乎没有合适的方法满足我的需求。那么该怎么办呢?
Regex.Split("1,2,3", ",")
将返回数组["1","2","3"]
。您提供的模式定义了分隔符,而不是您想要保留的内容。在这里,Regex.Split
不是您想要使用的工具。您正在尝试保留文本和分隔符,这不是Split
的作用。 - Jim Mischel