简体   繁体   中英

Why my Regex expression doesn't work?

I have this program:

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

using System.Xml;
using System.Text.RegularExpressions;
using System.IO;
using System.Net;

namespace Reviews_browser_test
{
    class Program
    {
        static void Main(string[] args)
        {

            Console.WriteLine(enter good, that u want to find: ");
            string tovar = Console.ReadLine();
            string page = "http://www.ulmart.ru/search?string=&rootCategory=&sort=6";
            page = page.Insert(35, tovar); // inserts good's id into url


            HttpWebRequest site = (HttpWebRequest)WebRequest.Create(page);

            HttpWebResponse response = (HttpWebResponse)site.GetResponse();
            Stream dataStream = response.GetResponseStream();
            StreamReader read = new StreamReader(dataStream);
            String data = read.ReadToEnd();
            Console.WriteLine(data);

            System.IO.File.WriteAllText("ulmart.html", data);

            Console.ReadKey();


            Match m;


            string pattern = "<span[^>]*?>[0-9]{4,10}</span>";


            m = Regex.Match(data, pattern);
            while (m.Success)
            {
                Console.WriteLine("Found an id " + m.Groups[1] + " at string "+ m.Groups[1].Index);
                m = m.NextMatch();
            }

            Console.ReadKey();
        }
    }
}

And I want to get all id numbers from the html file. But i don't know, why using this regex it doesn't find anything, while notepad++ finds each id fine. The example of html string, that should be found, using this regex:

<span class="num">3609304</span>

Where is my mistake?

The best way to solve the issue is to use HtmlAgilityPack . Install it as a NuGet package, and use the following method:

public List<string> HtmlAgilityPackGetNumericSpan4to10(string html)
{
        var vals = new List<string>();
        HtmlAgilityPack.HtmlDocument hap;
        Uri uriResult;
        if (Uri.TryCreate(html, UriKind.Absolute, out uriResult) 
                            && uriResult.Scheme == Uri.UriSchemeHttp)
        { // html is a URL 
            var doc = new HtmlAgilityPack.HtmlWeb();
            hap = doc.Load(uriResult.AbsoluteUri);
        }
        else
        { // html is a string
            hap = new HtmlAgilityPack.HtmlDocument();
            hap.LoadHtml(html);
        }
        var nodes = hap.DocumentNode.SelectNodes("//span[@class='num']");
        if (nodes != null)
        {
            foreach (var node in nodes)
            {
                var val = node.InnerText;
                if (val.ToCharArray().All(p => Char.IsDigit(p)) 
                                 && val.Length >= 4 && val.Length <= 10)
                    vals.Add(val);
            }
        }
        return vals;
}

With "//span[@class='num']" we collect only the span tags that have class attribute value equal to num . With if (val.ToCharArray().All(p => Char.IsDigit(p)) && val.Length >= 4 && val.Length <= 10) we check if the inner text is all numeric and its length is from 4 to 10.

Result with just your example string:

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM