正则表达式实现最小匹配
发布日期:2021-05-14 04:37:30 浏览次数:18 分类:博客文章

本文共 1050 字,大约阅读时间需要 3 分钟。

正则表达式默认情况下实现的是最大化匹配,这在有些情况下是非常不愿意出现的,比如下面这段代码:

# 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;             }

上一篇:基于异步方式的语法着色器
下一篇:关于利用VS2008创建项目遇到的小困惑备忘

发表评论

最新留言

不错!
[***.144.177.141]2025年04月24日 12时17分46秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章