Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在字符串中查找图像url_C# - Fatal编程技术网

C# 在字符串中查找图像url

C# 在字符串中查找图像url,c#,C#,我的程序旨在根据特定关键字搜索字符串中的图像url。它实际上工作正常,唯一的问题是搜索未找到错误。 出于某种原因,它就像代码没有到达这个if,如果没有找到匹配的最后if,它不会返回任何错误 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Net; using System.Text.Reg

我的程序旨在根据特定关键字搜索字符串中的图像url。它实际上工作正常,唯一的问题是搜索未找到错误。 出于某种原因,它就像代码没有到达这个if,如果没有找到匹配的最后if,它不会返回任何错误

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Text.RegularExpressions;

namespace ConsoleApplication3
{
    class Program
    {
        static void Main(string[] args)
        {
            using (WebClient client = new WebClient()) 
            {
                int count = 0;
                Regex SearchItem = new Regex("http://.+?\\.jpg");
                string SearchValue = "fgdfgdf";
                string htmlCode = "fsdflkjsdfkjsdfkjdsflkhttp://www.dssdtanya.jpgfsdf;ldsmfs;dlfms;dmfs";
                Match matches = SearchItem.Match(htmlCode);

                while (matches.Success) 
                {
                    string test = matches.ToString();
                    if (test.Contains(SearchValue))
                    {
                        count++;
                        Console.WriteLine("Result #{0}: '{1}' found in the source code at position {2}.",count, matches.Value, matches.Index);
                        matches = matches.NextMatch();
                    }
                }

                Console.WriteLine(count);
                if (count == 0) { Console.WriteLine("search not found."); }
                Console.ReadKey();
            }
        }
    }
}

如果第一个测试不包含搜索值,则程序将进入无限循环。将代码更改为:

while (matches.Success)
{
    string test = matches.ToString();
    if (test.Contains(SearchValue))
    {
        count++;
        Console.WriteLine("Result #{0}: '{1}' found in the source code at position {2}.", count, matches.Value, matches.Index);
    }
    matches = matches.NextMatch(); //moved this outside the if
}
这可能对你有帮助