本文實例講述了正則表達式實現最小匹配功能的方法。分享給大家供大家參考,具體如下:
正則表達式默認情況下實現的是最大化匹配,這在有些情況下是非常不愿意出現的,比如下面這段代碼:
# starting IndiaInventoryAPP.exe" ~~DisplayVariableValues "parameterGroup,mailRecipients,ModuleArgs"~DisplayVariableValues "LogFolder"~$binaryExitCode = 0~~$IndiaInventoryArgs = "-asWin32Console -S HKDRMSUAT3 -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~
我們想匹配#與~中間的任何文字,實現最小匹配的方法就是利用(?i)
下面是具體實現方法:
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;}
希望本文所述對大家正則表達式學習有所幫助。
新聞熱點
疑難解答