C # method of fetching a tag link and innerhtml based on regular expression

Source: Internet
Author: User
This article mainly introduces C # method of fetching a tag link and innerhtml based on regular expression, and analyzes C # using regular expression to carry on the matching and grasping of the page element with the example form, and the friends can refer to the following

The example in this paper describes C # 's method of fetching a tag link and innerhtml based on regular expressions. Share to everyone for your reference, as follows:

Read page htmlstring text = file.readalltext (environment.currentdirectory + "//test.txt", Encoding.GetEncoding ("gb2312") ); string prttern = "<a (\\s+ (href=\" (?<url> ([^\]) *) \ "|" ([^ ']) * ' |\\w+=\ "(([^\]) *) \" | " ([^ ']) *)) +> (?<text> (. *?)) </a> var maths = regex.matches (text, prttern);//Fetch the file to be written using (FileStream w = new FileStream ( Environment.currentdirectory + "//wirter.txt", FileMode.Create)) {for    (int i = 0; I < maths. Count; i++)    {      byte[] bs = Encoding.UTF8.GetBytes (string. Format ("link address: {0},  innerhtml:{1}", Maths[i]. groups["url"]. Value,        Maths[i]. groups["Text"]. Value) + "\ r \ n");      W.write (BS, 0, BS. Length);      Console.WriteLine ();    }} Console.readkey ();

Graphical regularization

Friends need to intercept the IMG tag src and data-url similar to the above. Incidentally attached

String text =file.readalltext (environment.currentdirectory + "//test.txt", Encoding.GetEncoding ("gb2312")); string Prttern = "[^\ "]*?) \ "|data-url=\" (? <dataurl>[^\ "]*?) \"| [-\\w]+=\ "[^\"]*?\ ")) *\\s*/>"; var maths = regex.matches (text, prttern);//Fetch the file to be written using (FileStream w = new FileStream (environment.currentdirectory + "//wirter.txt", FileMode.Create)) {for    (int i = 0; I < maths. Count; i++)    {      byte[] bs = Encoding.UTF8.GetBytes (string. Format ("Picture src:{0},  picture Data-url:{1}", Maths[i]. groups["src"]. Value,        Maths[i]. groups["Dataurl"]. Value) + "\ r \ n");      W.write (BS, 0, BS. Length);      Console.WriteLine ();    }}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.