Using regular expressions in. Net

Source: Internet
Author: User

Micsoft's. NET already supports regular expressions, and you can use the frame class regex. If you use regular expressions to replace strings that match rules, there are some small differences in the use of visual c++.net and Visual C #. Net.

1. Visual c++.net

Use of namespaces: using namespace system::text::regularexpressions;

String *s = "Test text sample for regular expression";

(1) Define object: Regex *rx = new Regex ("t[^]*");//Match string starting with T, followed by 0 to n non-whitespace characters until a space end is encountered

Complete Match replacement: s = rx->replace (S, "");//Match to later, delete

(2) can also be used directly with the static function of replace to complete the above operation:

s = Regex::replace (S, "t[^]*", "");

2. Visual c#.net

Use of namespaces: using System.Text.RegularExpressions;

String s = "Test text sample for regular expression";

(1) Define object: Regex rx = new Regex ("t[^]*");//Match string starting with T, followed by 0 to n non-whitespace characters until a space end is encountered

Complete Match replacement: s = Rx. Replace (S, "");//Match to later, delete

(2) using static function Regex::replace to complete

s = Regex.Replace (S, "t[^]*", "");

Explanation: The difference between the two is mainly the difference of grammatical rules.

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.