C #: Obtain the regular expression matching the code on the webpage (obtain the value between the start and end strings in the string)

Source: Internet
Author: User
For example:
<Div> 1Div </div>
<A> 1A </a>
<P> 1 P </P>
<P> 2 P </P>
<Div> 2div </div>
<A> 2a </a>
<P> 3 P </P>
<P> 4 P </P>
<A> 3A </a>
<P> 5 p </P>
<Div> 3div </div>
<A> 4A </a>
<P> 6 P </P>
<Span> 1 span </span>

The problem is: there are n multiple Div, N multiple p, N multiple A tags and a maximum of 1 span, to obtain only the content in all P and the content in the last span (where the content obtained from P has a condition, that is, only the content of P with the previous a tag will be obtained.) span may not exist. If yes, it will be obtained. If no, it will not be obtained:

C # Regular Expression Using System. Text. regularexpressions;

 

 

Code

  String Restult =   "" ;
Foreach (Match m In RegEx. Matches (STR, @" (? INS )(? <= (</A> \ s * <(? <Mark> P [^>] *>) | <(? <Mark> span) [^>] *>) [\ s] +? (? = </\ K <mark>) " ))
{
Restult + = M. value; // Is the result you want

MessageBox. Show (M. value );
}

 

Or use Foreach (Match m In RegEx. Matches (yourhtml, @" (? Is) (</a> \ s * <(? <Mark> P [^>] *>) | <(? <Mark> span) [^>] *> )(? <DATA> [\ s] + ?) </\ K <mark> " ))
{
M. Groups [ " Data " ]. Value; //
}

 

Or >>>>>>Obtains the value between the start and end strings.

Code

# Region Obtains the value between the start and end strings.
///   <Summary>
/// Obtains the value between the start and end strings.
///   </Summary>
///   <Param name = "begin"> Start matching mark </Param>
///   <Param name = "end"> End match tag </Param>
///   <Param name = "html"> HTML string </Param>
///   <Returns> Returns an intermediate string. </Returns>
Public   Static Matchcollection getmidvalue ( String Begin, String End, String Html)
{
RegEx Reg =   New RegEx ( " (? <= ( "   + Begin +   " ) [. \ S] *? (? = ( "   + End +   " )) " , Regexoptions. multiline | Regexoptions. singleline );
Return Reg. Matches (HTML );
}
# Endregion

 

 

Code

  ///   <Summary>  
/// Obtains the value between the start and end strings.
///   </Summary>  
///   <Param name = "str"> </param>  
///   <Param name = "S"> Start </Param>  
///   <Param name = "E"> End </Param>  
///   <Returns> </returns>  
Private   String Getvalue ( String STR, String Start, String End)
{
RegEx RG =   New RegEx ( " (? <= ( "   + Start +   " ) [. \ S] *? (? = ( "   + End +   " )) " , Regexoptions. multiline | Regexoptions. singleline );

Return RG. Match (STR). value;
}

 

 

 

// Regular expression is used to extract a single table, which can be good according to a specific Identifier character in the table!

If only"Member Information"This is used as a reference mark, and I can use the above slightly modified, the problem is that, if" 00 "or" 444 "is used as a reference mark, considering the nesting of <Table> labels, we must ensure that the inmost <Table> containing the reference identity is obtained, and that the <Table> and </table> matching must be ensured.

 

Code

Match mm = RegEx. Match (HTML, @" <Table [^>] *> (<Table [^>] *> (? <O>) | </table> (? <-O>) | (?! </? Table) [\ s]) *) (? (O )(?!))) \ B "   +   " Member Information "   +   @" \ B (? :(?! <Table [^>] *>) [\ s]) *? (<Table [^>] *> (? <O>) | </table> (? <-O>) | (?! </? Table) [\ s]) *) (? (O )(?!))) </Table> " , Regexoptions. ignorecase );

 

If a regular expression has special characters in the input reference identity, it must be preprocessed.Program.
If multiple input reference IDs appear in the source string, the <Table>

// Extract the content in TB from a single table using regular expressions .........

Code

Match mm = RegEx. Match (HTML, @" <Table [^>] *> (<Table [^>] *> (? <O>) | </table> (? <-O>) | (?! </? Table) [\ s]) *) (? (O )(?!))) \ B "   +   " Member winning/losing Materials "   +   @" \ B (? :(?! <Table [^>] *>) [\ s]) *? (<Table [^>] *> (? <O>) | </table> (? <-O>) | (?! </? Table) [\ s]) *) (? (O )(?!))) </Table> " , Regexoptions. ignorecase );
If (Mm. Success)
{
// MessageBox. Show (Mm. value );

// Matchcollection MDD = getmidvalue ("<TD", "</TD>", mm. value );
// Foreach (Match m in MDD)
// {
// For (INT I = 1; I <M. Groups. Count; I ++)
// {
// Restult + = M. Groups [I]. value; // Is the result you want
// }
// }

Matchcollection MC = RegEx. Matches (Mm. value, @" <TD [^>] *> \ s *(? <Content> [\ s] *?) \ S * </TD> " , Regexoptions. ignorecase );
Foreach (Match m In MC)
{
For ( Int I =   1 ; I < M. Groups. Count; I ++ )
{
Restult + = M. Groups [I]. Value +   " \ N " ;
}
}
MessageBox. Show (restult );
}

 

 

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.