Use regular expressions to automatically match and get the data you need in C # __ Regular expressions

Source: Internet
Author: User
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 >&lt ; 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
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.