![](/img/trans.png)
[英]C# regex: match a string starting with x and ending with y, not including the ending part & match the last occurence of a pattern
[英]Extracting a string starting with x and ending with y
首先,我对此进行了搜索,并且能够找到如何使用String.Split()之类的东西来根据条件提取字符串。 然而,我无法找到如何根据结束条件提取它。 例如,我有一个文件链接到图像: http://i594.photobucket.com/albums/tt27/34/444.jpghttp://i594.photobucket.com/albums/as/asfd/ghjk6.jpg
: http://i594.photobucket.com/albums/tt27/34/444.jpghttp://i594.photobucket.com/albums/as/asfd/ghjk6.jpg
: http://i594.photobucket.com/albums/tt27/34/444.jpghttp://i594.photobucket.com/albums/as/asfd/ghjk6.jpg
你会注意到所有图像都以http://
开头,以.jpg
结尾。 但是,.jpg由http://继承而没有空格,这使得这更难一点。
所以基本上我试图找到一种方法(正则表达式?)从以http://开头并以.jpg结尾的字符串中提取字符串
正则表达式是最简单的方法。 如果您不熟悉正则表达式,可以查看Regex Buddy 。 这是一个相对便宜的小工具,我发现在学习时非常有用。 对于您的特定情况,可能的表达式是:
(http://.+?\.jpg)
它可能需要更多的细化,因为有边界情况可以解决这个问题,但如果文件是一个简单的列表,它将起作用。
你也可以在这里免费快速测试表达式。
根据您的最新评论,如果您还有其他非图像的链接,那么您需要确保它不是从http://开始一个链接并且一直读到.jpg以获取下一个图像。 由于不允许URL有空格,您可以这样做:
(http://[^\s]+\.jpg)
这基本上说,“匹配以http://开头并以.jpg结尾的字符串,其中两者之间至少有一个字符,这些字符都不是空格”。
Regex RegexObj = new Regex("http://.+?\\.jpg");
Match MatchResults = RegexObj.Match(subject);
while (MatchResults.Success) {
//Do something with it
MatchResults = MatchResults.NextMatch();
}
在您的特定情况下,您可以始终按“.jpg”拆分。 您可能最终会在数组末尾添加一个空元素,并且如果需要,必须在每个文件的末尾附加.jpg。 除此之外,我认为它会起作用。
测试了以下代码,它工作正常:
public void SplitTest()
{
string test = "http://i594.photobucket.com/albums/tt27/34/444.jpghttp://i594.photobucket.com/albums/as/asfd/ghjk6.jpg";
string[] items = test.Split(new string[] { ".jpg" }, StringSplitOptions.RemoveEmptyEntries);
}
它甚至摆脱了空洞的进入......
以下LINQ将通过http:分隔,并确保仅获取以jpg结尾的值。
var images = from i in imageList.Split(new[] {"http:"},
StringSplitOptions.RemoveEmptyEntries)
where i.EndsWith(".jpg")
select "http:" + i;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.