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.