本文實(shí)例講述了正則表達(dá)式實(shí)現(xiàn)最小匹配功能的方法。分享給大家供大家參考,具體如下:
正則表達(dá)式默認(rèn)情況下實(shí)現(xiàn)的是最大化匹配,這在有些情況下是非常不愿意出現(xiàn)的,比如下面這段代碼:
# 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~
我們想匹配#與~中間的任何文字,實(shí)現(xiàn)最小匹配的方法就是利用(?i)
下面是具體實(shí)現(xiàn)方法:
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:這里再為大家提供2款非常方便的正則表達(dá)式工具供大家參考使用:
JavaScript正則表達(dá)式在線測(cè)試工具:
http://tools.VeVB.COm/regex/javascript
正則表達(dá)式在線生成工具:
http://tools.VeVB.COm/regex/create_reg
希望本文所述對(duì)大家正則表達(dá)式學(xué)習(xí)有所幫助。
新聞熱點(diǎn)
疑難解答