Regular Expressions can match various types of data (such as e-mail addresses, phone numbers, and identity numbers) according to the settings ). Regular Expressions are powerful and flexible to use. C #, Java, JavaScript, and other languages support regular expressions. Regular Expressions are indispensable for "capturing data" on the Internet. Today, I want to talk about using regular expressions in C # To automatically match and obtain the required data.
The following is a part (string) of the query result returned by an IP address query website. Now, you need to obtain "query result 1:" followed by "Beijing Netcom" from this string ". < TD Align = "Center" > < Ul > < Li > Query Result 1: Beijing Netcom </ Li > < Li > Query Result 2: Beijing Netcom </ Li > < Li > Query Result 3: Beijing Netcom </ Li > </ Ul > </ TD >
C # related Code As follows:String Srcstring = " <TD align = \ " Center \ " > <Ul> <li> query result 1: Beijing Netcom </LI> <li> query result 2: Beijing Netcom </LI> <li> query result 3: beijing Netcom </LI> </ul> </TD> " ;
//Match the regular expression of all characters between "<li> query result 1:" and "</LI>"
String Regexstr = " <Li> query result 1 :(? <Key> .*?) </LI> " ;
RegEx R = New RegEx (regexstr, regexoptions. None );
Match MC = R. Match (srcstring );
StringDatastr = Mc. Groups [ " Key " ]. Value;
Note:
The most important thing here is to set a regular expression:"<Li> query result 1 :(? <Key> .*?) </LI>".
character |
description |
|
Based on key gets the value matched by a regular expression. |
. |
Except " \ n " . |
* |
match the previous subexpression zero or multiple times . |
? |
follows any other delimiter (*, + ,?, {N },{ n ,},{ n, m}) later, the matching mode is not greedy ; the non-Greedy mode matches as few searched strings as possible, while the default greedy mode matches as many searched strings as possible. |
Datastr is the data we need: "Beijing Netcom ".
RelatedArticle: Use WebClient to automatically fill in and submit the ASP. NET page formSource code
Use WebClient to automatically fill in and submit ASP. NET page forms
Address: http://www.cnblogs.com/anjou/archive/2007/03/20/681744.html