Regular expressions can match various data according to Settings (e-mail address, phone number, ID number, etc.). Regular expressions are powerful, flexible, and support regular expressions in many languages, such as C#,java,javascript. "Crawl data" on the Internet is the most indispensable use of regular expressions. What I'm going to say today is using regular expressions to automatically match and get the data you need in C #.
From the following is an IP query site to return the query results of a part (string), now is to get from this string "Query Results 1:" Behind the "Beijing Netcom."
< td align = "center" >< ul >< li > Query Results 1: Beijing netcom </li >< li > Query Results 2: Beijing netcom </Li >< ; Li > Query Results 3: Beijing Netcom </Li ></ul ></td >
C # related code is as follows:
String srcstring = "<td align=/" center/><ul><li> Results 1: Beijing netcom </li><li> Query Results 2: Beijing Netcom & lt;/li><li> Query Results 3: Beijing netcom </li></ul></td> ";
Regular expressions that match 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);
String datastr = mc. groups["Key"]. Value;
Description
The key here is to set the regular expression: "<li> query Results 1: (? <key>.*?) </li> ".
Character
|
Describe
|
<key> |
Gets the value that is matched by the regular expression based on the key. |
. |
Matches any single character except "/n". |
* |
Matches the preceding subexpression 0 or more times. |
? |
followed by any other limiter (*, +,?, {n}, {n,}, {n,m}), the matching pattern is not greedy, the greedy pattern matches the string searched as little as possible, and the default greedy pattern matches as many of the searched strings as possible. |
DATASTR is the data we have: "Beijing netcom."
This article address: http://www.cnblogs.com/anjou/archive/2007/03/20/681744.html