
正则表达式实现最小匹配
发布日期: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; }
发表评论
最新留言
不错!
[***.144.177.141]2025年04月24日 12时17分46秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
Git简单理解与使用
2019-03-11
爬虫-01
2019-03-11
echarts 基本图表开发小结
2019-03-11
二分查找.基于有序数组的查找方法.704
2019-03-11
C语言文档操作
2019-03-11
制作JS验证码(简易)
2019-03-11
adb通过USB或wifi连接手机
2019-03-11
vue使用ecahrts词云图
2019-03-11
【README】回溯算法基本框架
2019-03-11
数组中常见的算法
2019-03-11
泛型机制 Generic
2019-03-11
包装类
2019-03-11
JDK9-15新特性
2019-03-11
集合继承结构
2019-03-11
ArrayList 实现类
2019-03-11
LinkedList 实现类
2019-03-11
Vector 实现类
2019-03-11
HashMap类、HashSet
2019-03-11
HashTable类
2019-03-11