C # Regular Expression delegation and Lambda expression usage

Source: Internet
Author: User

As mentioned above, the use of delegation in regular expressions is matchevaluator. For details, refer

This is the most standard syntax: to customize a function, and then use this function delegate as a parameter to instantiate the matchevaluator object. Finally, you can use this object for parameter input during regular expression matching, manual processing of matching results.

Today we will introduce two easy-to-use writing methods. The use case is as follows: Reduce the number enclosed in parentheses in the string by 10:
StringS ="Sdlfk (23984) sdlkf (3) sldfk (15) weoif23948 (fsdlk) fdskjf (34) dlfk (432d) <br/>";
Response. Write (s );
RegExR = new RegEx (@"(? <= \ () \ D + (? = \))");

// 1, Lambda expression
MatchevaluatorMe = m => (Int. Parse (M. Value)-10). tostring ();
Response. Write (R. Replace (S, me ));

// 2, anonymous delegate
Response. Write (R. Replace (S,Delegate(MatchM) {return (Int. Parse (M. Value)-10). tostring ();}));

The output is as follows:

Sdlfk (23984) sdlkf (3) sldfk (15) weoif23948 (fsdlk) fdskjf (34) dlfk (432d)
Sdlfk (23974) sdlkf (-7) sldfk (5) weoif23948 (fsdlk) fdskjf (24) dlfk (432d)
Sdlfk (23974) sdlkf (-7) sldfk (5) weoif23948 (fsdlk) fdskjf (24) dlfk (432d)

No problem

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.