本文實例講述了正則表達式實現(xià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款非常方便的正則表達式工具供大家參考使用:
JavaScript正則表達式在線測試工具:
http://tools.jb51.net/regex/javascript
正則表達式在線生成工具:
http://tools.jb51.net/regex/create_reg
希望本文所述對大家正則表達式學習有所幫助。
您可能感興趣的文章:- 正則表達式匹配任意字符(包括換行符)的寫法
- 正則表達式匹配不包含某些字符串的技巧
- 匹配yyyy-mm-dd日期格式的的正則表達式
- 匹配中文漢字的正則表達式介紹
- 正則表達式 匹配至少有一個非空白字符并且不超過指定長度
- js 正則表達式學習筆記之匹配字符串
- 正則表達式匹配 非XXX的行
- PHP匹配多行的正則表達式分析
- js正則表達式匹配數(shù)字字母下劃線等
- php用正則表達式匹配URL的簡單方法
- php正則表達式匹配img中任意屬性的方法
- 匹配任意字符的正則表達式寫法