Implementation of a regular expression extraction example (. net asp JavaScript/JS) for URLs, titles, and images

Source: Internet
Author: User

In some situations such as crawling and filtering, the advantage of the regular expression is obvious.
For example, there are the following strings: CopyCode The Code is as follows: <li> <a href = "http://www.abcxyz.com/something/article/143.htm" Title = "FCKeditor highlight code plug-in test"> <SPAN class = "Article-Date"> [09/11] </span> FCKeditor highlight code plug-in test </a> </LI>

now, you need to extract the URL following href, the date in [], and the link text.
The following describes how to implement C #, ASP, and Javascript.
Implementation of C # copy Code the code is as follows: string strhtml = "

  • [09/11] FCKeditor highlight code plug-in test
  • ";
    string pattern = "http: // ([^ \ s] + )\". +? Span. +? \ [(. + ?) \]. +?> (. + ?) <";
    RegEx Reg = new RegEx (pattern, regexoptions. ignorecase);
    matchcollection MC = reg. matches (strhtml);
    If (MC. count> 0)
    {< br> foreach (Match m in MC)
    {< br> console. writeline (M. groups [1]. value);
    console. writeline (M. groups [2]. value);
    console. writeline (M. groups [3]. value);
    }< BR >}

    Asp implementation copy Code the code is as follows: <%
    dim STR, Reg, objmatches
    STR = "

  • [09/11] FCKeditor highlight code plug-in test
  • "
    set Reg = new Regexp
    Reg. ignorecase = true
    Reg. global = true
    Reg. pattern = "Http: // ([^ \ s] +)" ". +? Span. +? \ [(. + ?) \]. +?> (. + ?) <"
    set objmatches = reg. execute (STR)
    If objmatches. count> 0 then
    response. write ("url:")
    response. write (objmatches (0 ). submatches (0)
    response. write ("
    ")
    response. write ("Date:")
    response. write (objmatches (0 ). submatches (1)
    response. write ("
    ")
    response. write ("title:")
    response. write (objmatches (0 ). submatches (2)
    end if
    %>

    JavaScript implementation copy Code the code is as follows:

    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.