正則表達(dá)式實現(xiàn)最小匹配功能的方法
本文實例講述了正則表達(dá)式實現(xiàn)最小匹配功能的方法。分享給大家供大家參考,具體如下:
正則表達(dá)式默認(rèn)情況下實現(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~
我們想匹配#與~中間的任何文字,實現(xiàn)最小匹配的方法就是利用(?i)
下面是具體實現(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á)式在線測試工具:
http://tools.jb51.net/regex/javascript
正則表達(dá)式在線生成工具:
http://tools.jb51.net/regex/create_reg
希望本文所述對大家正則表達(dá)式學(xué)習(xí)有所幫助。
相關(guān)文章
自動檢測數(shù)字替換非數(shù)字的正則表達(dá)式
這篇文章主要介紹了自動檢測數(shù)字替換非數(shù)字的正則表達(dá)式 ,需要的朋友可以參考下2016-05-05應(yīng)該如何構(gòu)造復(fù)雜的正則表達(dá)式
昨天Snopo問我如何寫一段正則表達(dá)式,來提取sql的條件語句。解答之余,想寫一篇文章介紹一下經(jīng)驗2012-09-09