正则表达式实现最小匹配功能的方法
本文实例讲述了正则表达式实现最小匹配功能的方法。分享给大家供大家参考,具体如下:
正则表达式默认情况下实现的是最大化匹配,这在有些情况下是非常不愿意出现的,比如下面这段代码:
# 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:cs48516posIds.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; }
PS:这里再为大家提供2款非常方便的正则表达式工具供大家参考使用:
JavaScript正则表达式在线测试工具:
http://tools.gimoo.net/regex/javascript
正则表达式在线生成工具:
http://tools.gimoo.net/regex/create_reg
希望本文所述对大家正则表达式学习有所帮助。
手机号码验证方法(正则验证)
只允许输入数字xsl:attributename="onkeyup"value=value.replace(/[^d]/g,'')/xsl:attributexsl:attributename="onbeforepaste"clipboardData.setData('text',clipboardData.getData('text').replace(/[^d]
利用正则表达式提取固定字符之间的字符串
前言在JS的正则零宽断言匹配中,只支持前瞻匹配,不支持后瞻。这就尴尬了,因为在业务当中,我们大多时候是知道了要目标数据的前后缀字符串,但
js中使用正则表达式查找字母和数字的方法
废话不多说,先看下js中使用正则查找字母和数字的方法,具体代码如下所示:!DOCTYPEHTMLhtmlheadmetacharset="UTF-8"title/titlescripttype="text/javascript"window.onload=func