The regular expression is used to obtain the content in the HTML Tag. (Note: If the content contains a carriage return, please refer to the third example)

Source: Internet
Author: User
Several Methods for getting simple HTML Tag content
// ==================================== Begin1 ====================== ==========
// Test string
String strtmp = string. empty;
// Regular Expression
String tmpstr = string. empty;

// Retrieve the value of the matching item in the specified HTML Tag. regexoptions. ignorecase is case-insensitive, and regexoptions. multiline is case-insensitive,
// Tmpstr = "<title> ([^ <] *) </title>" // obtain the content between <title>

Strtmp = @ "<add key = 'ononstring' value = 'server = localhost; database = database name; uid = sa; Pwd =; pooling = true'/> ";
// Obtain the string between "database =" and ";": Database = (.*);
Tmpstr = "database = ([^;] *);";
Match titlematch = RegEx. Match (strtmp, tmpstr, regexoptions. ignorecase | regexoptions. multiline );

// The following example is used as a syntax reference to obtain the size value. The actual application may not be so complex.
Strtmp = "> <font color = 'red' size = 6> webform3 </font> <";
Tmpstr = @ "<(\ W + \ s + \ W + [=] + ['] + \ W + ['] + \ s + [size =] + \ D )> ";
Match titlematch = RegEx. Match (strtmp, tmpstr, regexoptions. ignorecase | regexoptions. multiline );


// Retrieve the matching Value
String tmpstrtitle = titlematch. Groups [1]. value;
// Replace all HTML tags on the HTML page
Label1.text = RegEx. Replace (label1.text. Trim (), "<. +?> "," ") +" ********* "+ Titlematch. Groups [1]. value;

// Determine whether the matching regular expression is successful
If (RegEx. Match (tmpstr, "<. +?> "). Success)
{
// Operation
}
// ==================================== End1 ======================== ==========

// ==================================== Begin2 ========================== ==========
String webdoccontent = "<a href = http://www.xxx.xxx/college/pages/default.htm target = _ blank> faculty </a> ";
// Explain the following regular expression: [\ s] indicates matching space characters, and "+" indicates connection
String strpattern = @ "A [\ s] + href = (? <Link> [^ \ s>] +) [^>] *> (? <Text> [^ <] *) </a> ";
// Obtain the text displayed by the link
Matchcollection matches = RegEx. Matches (webdoccontent, strpattern, regexoptions. ignorecase | regexoptions. Compiled );
Foreach (match nextmatch in matches)
{
String url = nextmatch. Groups ["Link"]. value. tostring (). Trim ();
String urltext = nextmatch. Groups ["text"]. value. tostring (). Trim ();
Response. Write (URL + "****");
Response. Write (urltext );
}
// ==================================== End2 ========================== ==========

// ==================================== Begin3 ========================== ==========
String strpagecontent = string. empty;
Streamreader srpage = new streamreader (@ "E: \ save.txt", system. Text. encoding. getencoding ("gb2312 "));
Strpagecontent = srpage. readtoend ();
Srpage. Close ();
// (\ S) * Indicates 0 or multiple space characters and carriage return characters, and * Indicates 0 or more matching characters. (.*?) Indicates all information except the carriage return.
Matchcollection titlematchs = RegEx. Matches (strpagecontent, "<TD width = \" 85% \ "class = \" common_text \ "> (\ s )*(.*?) (\ S )*(.*?) (\ S )*(.*?) (\ S )*(.*?) (\ S) *) </TD> ", regexoptions. ignorecase | regexoptions. multiline );
Int tmpnum = 0;
// The content set that meets the regular expression of the loop.
Foreach (match nextmatch in titlematchs)
{
++ Tmpnum;
Label1.text + = tmpnum + "<br> *****" + nextmatch. Groups [1]. value;
}
// ==================================== End3 ======================= ==========

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.