A regular expression is used to implement the minimum matching function.

Source: Internet
Author: User

A regular expression is used to implement the minimum matching function.

This example describes how to use a regular expression to implement the minimum matching function. We will share this with you for your reference. The details are as follows:

By default, regular expressions implement maximized matching, which is quite undesirable in some cases, such as the following code:

# Starting IndiaInventoryAPP.exe "~~ DisplayVariableValues "parameterGroup, mailRecipients, ModuleArgs "~ DisplayVariableValues "LogFolder "~ $ BinaryExitCode = 0 ~~ $ IndiaInventoryArgs = "-asWin32Console-S release-D $ DatabaseName-U $ DatabaseUserName-P $ DatabasePassword-L $ LogFolder-MailRecipients $ mailRecipients-T $ today_yyyy-z d: \ cs48516 \ posIds.txt "~ ExecuteBinaryCommand ([ref] $ binaryExitCode) "$ applicationPath/IndiaInventoryAPP.exe" $ IndiaInventoryArgs $ true ~

We want to match # ~ The minimum matching method for any text in the middle is to use (? I)

The specific implementation method is as follows:

string commentGrammer = @"(?i)\#.*?~";Regex commentRegex = new Regex(commentGrammer,RegexOptions.IgnoreCase|RegexOptions.Singleline);MatchCollection commentMC = commentRegex.Matches(input);foreach (Match match in commentMC){  int length = match.Length;  int index = match.Index;  richTextBox.Select(index, length);  richTextBox.SelectionColor = Color.Green;}

PS: here we will provide two very convenient Regular Expression tools for your reference:

JavaScript Regular Expression online testing tool:
Http://tools.jb51.net/regex/javascript

Regular Expression generation tool:
Http://tools.jb51.net/regex/create_reg

I hope this article will help you learn regular expressions.

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.