Use regular expressions in C # To automatically match and obtain the required data.

Source: Internet
Author: User
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 ".

From: http://www.cnblogs.com/anjou/archive/2007/03/20/681744.html

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.