📄 reg.htm
字号:
<DIV class=hl-main><SPAN style="COLOR: gray">^</SPAN><SPAN style="COLOR: olive">[</SPAN><SPAN style="COLOR: blue">A</SPAN><SPAN style="COLOR: gray">-</SPAN><SPAN style="COLOR: blue">Za</SPAN><SPAN style="COLOR: gray">-</SPAN><SPAN style="COLOR: blue">z0</SPAN><SPAN style="COLOR: gray">-</SPAN><SPAN style="COLOR: maroon">9</SPAN><SPAN style="COLOR: olive">]</SPAN><SPAN style="COLOR: gray">+$</SPAN></DIV></DIV>
<P>匹配由数字、26个英文字母或者下划线组成的字符串</P>
<DIV class=hl-surround>
<DIV class=hl-main><SPAN style="COLOR: gray">^\</SPAN><SPAN style="COLOR: blue">w</SPAN><SPAN style="COLOR: gray">+$</SPAN></DIV></DIV>
<P>匹配email地址</P>
<DIV class=hl-surround>
<DIV class=hl-main><SPAN style="COLOR: gray">^</SPAN><SPAN style="COLOR: olive">[</SPAN><SPAN style="COLOR: gray">\</SPAN><SPAN style="COLOR: blue">w</SPAN><SPAN style="COLOR: gray">-</SPAN><SPAN style="COLOR: olive">]</SPAN><SPAN style="COLOR: gray">+</SPAN><SPAN style="COLOR: olive">(</SPAN><SPAN style="COLOR: gray">\.</SPAN><SPAN style="COLOR: olive">[</SPAN><SPAN style="COLOR: gray">\</SPAN><SPAN style="COLOR: blue">w</SPAN><SPAN style="COLOR: gray">-</SPAN><SPAN style="COLOR: olive">]</SPAN><SPAN style="COLOR: gray">+</SPAN><SPAN style="COLOR: olive">)</SPAN><SPAN style="COLOR: gray">*@</SPAN><SPAN style="COLOR: olive">[</SPAN><SPAN style="COLOR: gray">\</SPAN><SPAN style="COLOR: blue">w</SPAN><SPAN style="COLOR: gray">-</SPAN><SPAN style="COLOR: olive">]</SPAN><SPAN style="COLOR: gray">+</SPAN><SPAN style="COLOR: olive">(</SPAN><SPAN style="COLOR: gray">\.</SPAN><SPAN style="COLOR: olive">[</SPAN><SPAN style="COLOR: gray">\</SPAN><SPAN style="COLOR: blue">w</SPAN><SPAN style="COLOR: gray">-</SPAN><SPAN style="COLOR: olive">]</SPAN><SPAN style="COLOR: gray">+</SPAN><SPAN style="COLOR: olive">)</SPAN><SPAN style="COLOR: gray">+$</SPAN></DIV></DIV>
<P>匹配url</P>
<DIV class=hl-surround>
<DIV class=hl-main><SPAN style="COLOR: gray">^</SPAN><SPAN style="COLOR: olive">[</SPAN><SPAN style="COLOR: blue">a</SPAN><SPAN style="COLOR: gray">-</SPAN><SPAN style="COLOR: blue">zA</SPAN><SPAN style="COLOR: gray">-</SPAN><SPAN style="COLOR: blue">z</SPAN><SPAN style="COLOR: olive">]</SPAN><SPAN style="COLOR: gray">+:</SPAN><SPAN style="COLOR: #ffa500">//匹配(\w+(-\w+)*)(\.(\w+(-\w+)*))*(\?\S*)?$</SPAN></DIV></DIV>
<P>匹配html tag</P>
<DIV class=hl-surround>
<DIV class=hl-main><SPAN style="COLOR: gray"><\</SPAN><SPAN style="COLOR: blue">s</SPAN><SPAN style="COLOR: gray">*</SPAN><SPAN style="COLOR: olive">(</SPAN><SPAN style="COLOR: gray">\</SPAN><SPAN style="COLOR: blue">S</SPAN><SPAN style="COLOR: gray">+</SPAN><SPAN style="COLOR: olive">)(</SPAN><SPAN style="COLOR: gray">\</SPAN><SPAN style="COLOR: blue">s</SPAN><SPAN style="COLOR: olive">[</SPAN><SPAN style="COLOR: gray">^></SPAN><SPAN style="COLOR: olive">]</SPAN><SPAN style="COLOR: gray">*</SPAN><SPAN style="COLOR: olive">)</SPAN><SPAN style="COLOR: gray">?></SPAN><SPAN style="COLOR: olive">(</SPAN><SPAN style="COLOR: gray">.*?</SPAN><SPAN style="COLOR: olive">)</SPAN><SPAN style="COLOR: gray"><\</SPAN><SPAN style="COLOR: blue">s</SPAN><SPAN style="COLOR: gray">*\</SPAN><SPAN style="COLOR: #8b0000">/</SPAN><SPAN style="COLOR: red">\1\s*></SPAN></DIV></DIV>
<P>Visual Basic & C# Regular Expression<BR>1.确认有效电子邮件格式<BR>下面的示例使用静态 Regex.IsMatch 方法验证一个字符串是否为有效电子邮件格式。如果字符串包含一个有效的电子邮件地址,则 IsValidEmail 方法返回 true,否则返回 false,但不采取其他任何操作。您可以使用 IsValidEmail,在应用程序将地址存储在数据库中或显示在 ASP.NET 页中之前,筛选出包含无效字符的电子邮件地址。</P>
<P>[Visual Basic]</P>
<DIV class=hl-surround>
<DIV class=hl-main>Function IsValidEmail(strIn As String) As Boolean<BR>' Return true if strIn is in valid e-mail format.<BR>Return Regex.IsMatch(strIn, ("^([\w-\.]+)@((\[[0-9]{ 1,3 }\.[0-9]{ 1,3 }\.[0-9]{ 1,3 }\.)|(([\w-]+\.)+))([a-zA-Z]{ 2,4 }|[0-9]{ 1,3 })(\]?)$")<BR>End Function</DIV></DIV>
<P>[C#]</P>
<DIV class=hl-surround>
<DIV class=hl-main>bool IsValidEmail(string strIn)<BR>{<BR>// Return true if strIn is in valid e-mail format.<BR>return Regex.IsMatch(strIn, @"^([\w-\.]+)@((\[[0-9]{ 1,3 }\.[0-9]{ 1,3 }\.[0-9]{ 1,3 }\.)|(([\w-]+\.)+))([a-zA-Z]{ 2,4 }|[0-9]{ 1,3 })(\]?)$");<BR>}</DIV></DIV>
<P>2.清理输入字符串<BR>下面的代码示例使用静态 Regex.Replace 方法从字符串中抽出无效字符。您可以使用这里定义的 CleanInput 方法,清除掉在接受用户输入的窗体的文本字段中输入的可能有害的字符。CleanInput 在清除掉除 @、-(连字符)和 .(句点)以外的所有非字母数字字符后返回一个字符串。</P>
<P>[Visual Basic]</P>
<DIV class=hl-surround>
<DIV class=hl-main>Function CleanInput(strIn As String) As String<BR>' Replace invalid characters with empty strings.<BR>Return Regex.Replace(strIn, "[^\w\.@-]", "")<BR>End Function</DIV></DIV>
<P>[C#]</P>
<DIV class=hl-surround>
<DIV class=hl-main>String CleanInput(string strIn)<BR>{<BR> // Replace invalid characters with empty strings.<BR> return Regex.Replace(strIn, @"[^\w\.@-]", "");<BR>}</DIV></DIV>
<P>3.更改日期格式<BR>以下代码示例使用 Regex.Replace 方法来用 dd-mm-yy 的日期形式代替 mm/dd/yy 的日期形式。</P>
<P>[Visual Basic]</P>
<DIV class=hl-surround>
<DIV class=hl-main>Function MDYToDMY(input As String) As String<BR>Return Regex.Replace(input, _<BR>"\b(?<month>\d{ 1,2 })/(?<day>\d{ 1,2 })/(?<year>\d{ 2,4 })\b", _<BR>"${ day }-${ month }-${ year }")<BR>End Function</DIV></DIV>
<P>[C#]</P>
<DIV class=hl-surround>
<DIV class=hl-main>String MDYToDMY(String input)<BR>{<BR> return Regex.Replace(input,"\\b(?<month>\\d{ 1,2 })/(?<day>\\d{ 1,2 })/(?<year>\\d{ 2,4 })\\b","${ day }-${ month }-${ year }");<BR>}</DIV></DIV>
<P>Regex 替换模式<BR>本示例说明如何在 Regex.Replace 的替换模式中使用命名的反向引用。其中,替换表达式 ${ day } 插入由 (?…) 组捕获的子字符串。</P>
<P>有几种静态函数使您可以在使用<a href="http://www.jb51.net/list/list_6_1.htm" target="_blank"><font color=red>正则</font></a>表达式操作时无需创建显式<a href="http://www.jb51.net/list/list_6_1.htm" target="_blank"><font color=red>正则</font></a>表达式对象,而 Regex.Replace 函数正是其中之一。如果您不想保留编译的<a href="http://www.jb51.net/list/list_6_1.htm" target="_blank"><font color=red>正则</font></a>表达式,这将给您带来方便</P>
<P>4.提取 URL 信息<BR>以下代码示例使用 Match.Result 来从 URL 提取协议和端口号。例如,“http://www.penner.cn:8080……将返回“http:8080”。</P>
<P>[Visual Basic]</P>
<DIV class=hl-surround>
<DIV class=hl-main>Function Extension(url As String) As String<BR>Dim r As New Regex("^(?<proto>\w+)://[^/]+?(?<port>:\d+)?/", _<BR>RegexOptions.Compiled)<BR>Return r.Match(url).Result("${ proto }${ port }")<BR>End Function</DIV></DIV>
<P>[C#]</P>
<DIV class=hl-surround>
<DIV class=hl-main>String Extension(String url)<BR>{<BR> Regex r = new Regex(@"^(?<proto>\w+)://[^/]+?(?<port>:\d+)?/",<BR> RegexOptions.Compiled);<BR> return r.Match(url).Result("${ proto }${ port }");<BR>}</DIV></DIV>
<P>只有字母和数字,不小于6位,且数字字母都包含的密码的<a href="http://www.jb51.net/list/list_6_1.htm" target="_blank"><font color=red>正则</font></a>表达式<BR>在C#中,可以用这个来表示:</P>
<DIV class=hl-surround>
<DIV class=hl-main><SPAN style="COLOR: #8b0000">"</SPAN><SPAN style="COLOR: red">\w{ 6 }(\w+)*</SPAN><SPAN style="COLOR: #8b0000">"</SPAN></DIV></DIV>
<P>一个将需要将路径字符串拆分为根目录和子目录两部分的算法程序,考虑路径格式有:C:\aa\bb\cc ,\\aa\bb\cc , ftp://aa.bb/cc 上述路径将分别被拆分为:C:\和aa\bb\cc ,\\aa 和 \bb\cc , ftp:// 和 aa.bb/cc 用javascript实现如下:</P>
<DIV class=hl-surround>
<DIV class=hl-main><SPAN style="COLOR: green">var</SPAN><SPAN style="COLOR: gray"> </SPAN><SPAN style="COLOR: blue">strRoot</SPAN><SPAN style="COLOR: gray">,</SPAN><SPAN style="COLOR: blue">strSub</SPAN><SPAN style="COLOR: gray"><BR></SPAN><SPAN style="COLOR: green">var</SPAN><SPAN style="COLOR: gray"> </SPAN><SPAN style="COLOR: blue">regPathParse</SPAN><SPAN style="COLOR: gray">=</SPAN><SPAN style="COLOR: #8b0000">/</SPAN><SPAN style="COLOR: red">^([^\\^</SPAN><SPAN style="COLOR: navy">\/</SPAN><SPAN style="COLOR: red">]+[\\</SPAN><SPAN style="COLOR: navy">\/</SPAN><SPAN style="COLOR: red">]+|\\\\[^\\]+)(.*)$</SPAN><SPAN style="COLOR: #8b0000">/</SPAN><SPAN style="COLOR: gray"><BR></SPAN><SPAN style="COLOR: green">if</SPAN><SPAN style="COLOR: olive">(</SPAN><SPAN style="COLOR: blue">regPathParse</SPAN><SPAN style="COLOR: gray">.</SPAN><SPAN style="COLOR: blue">test</SPAN><SPAN style="COLOR: olive">(</SPAN><SPAN style="COLOR: blue">strFolder</SPAN><SPAN style="COLOR: olive">))</SPAN><SPAN style="COLOR: gray"><BR></SPAN><SPAN style="COLOR: olive">{</SPAN><SPAN style="COLOR: gray"><BR> </SPAN><SPAN style="COLOR: blue">strRoot</SPAN><SPAN style="COLOR: gray">=</SPAN><SPAN style="COLOR: teal">RegExp</SPAN><SPAN style="COLOR: gray">.$</SPAN><SPAN style="COLOR: maroon">1</SPAN><SPAN style="COLOR: gray"><BR> </SPAN><SPAN style="COLOR: blue">strSub</SPAN><SPAN style="COLOR: gray">=</SPAN><SPAN style="COLOR: teal">RegExp</SPAN><SPAN style="COLOR: gray">.$</SPAN><SPAN style="COLOR: maroon">2</SPAN><SPAN style="COLOR: gray"><BR></SPAN><SPAN style="COLOR: olive">}</SPAN></DIV></DIV>
<div class="adall tc"><script type="text/javascript" src="http://img.jb51.net/imgby/con_bottom.js"></script></div>
<!--NEWSZW_HZH_END-->
<div id="art_xg">
<div id="con_da2"></div>
</div>
<div id="numstyle"></div>
<div id="con_bo"></div>
</div>
<div class=blank3></div>
<div class="tags">
<strong>Tags:</strong><a href="/tag/%D5%FD%D4%F2%B1%ED%B4%EF%CA%BD/1.htm" target="_blank" title="搜索关于正则表达式的文章">正则表达式</a>
</div>
<div class="sm"><script type="text/javascript" src="http://img.jb51.net/inc/sm.js"></script></div>
<div class="con_ff">
浏览次数:<span class="shownum" id="thehit">载入中...</span> <a href="javascript:doPrint();" class="button_link">打印本文</a><a href="javascript:window.opener=null;window.close();" class="button_link">关闭本文</a><a href='/index.htm' class=button_link>返回首页</a>
</div>
<div class="blank3"></div>
<span id="artkeys_tp"><script type="text/javascript" src="http://img.jb51.net/imgby/artkeys.js"></script></span>
<span id="con_tj_tp"><script type="text/javascript" src="http://img.jb51.net/imgby/baidu336.js"></script></span>
<span id="con_da8_tp"><script type="text/javascript" src="http://img.jb51.net/imgby/baidu234.js"></script></span>
<script type="text/javascript">
$("artkeys").innerHTML=$("artkeys_tp").innerHTML;
$("artkeys_tp").innerHTML="";
$("con_da1").innerHTML=$("con_tj_tp").innerHTML;
$("con_tj_tp").innerHTML="";
$("con_da8").innerHTML=$("con_da8_tp").innerHTML;
$("con_da8_tp").innerHTML="";
</script>
<script type="text/javascript">
document.body.oncopy = function () {
setTimeout( function () {
var text = clipboardData.getData("text");
if (text) {
text = text + "\r\n本文来自: 脚本之家(www.jb51.net) 详细出处参考:"+location.href; clipboardData.setData("text", text);
}
}, 100 )
}
</script>
<div class=blank3></div>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -