C#匹配中文字符串的4种正则表达式
<div align="left"><p style="margin: 10px auto; list-style-type: none; list-style-image: none;"><span style="font-family: "Microsoft YaHei";">在C#中,匹配中文的正则表达式用Unicode来表示时,范围是: [\u4e00-\u9fa5]。所以,在此基础上,我们可以得到如下一些正则表达式。</span></p><p style="margin: 10px auto; list-style-type: none; list-style-image: none;"><span style="font-family: "Microsoft YaHei";"><strong>1、匹配字符串全部是中文字符的正则表达式</strong></span></p><div class="codetitle"><span style="font-family: "Microsoft YaHei";">代码如下:</span></div><div id="code75323" class="codebody"><span style="font-family: "Microsoft YaHei";">"^[\u4e00-\u9fa5]+$"</span></div><p style="margin: 10px auto; list-style-type: none; list-style-image: none;"><br><span style="font-family: "Microsoft YaHei";">说明:“^”表示字符串开头,“$”表示字符串结束,“[\u4e00-\u9fa5]+”表示一个或多个中文字符。</span></p><p style="margin: 10px auto; list-style-type: none; list-style-image: none;"><span style="font-family: "Microsoft YaHei";"> </span></p><p style="margin: 10px auto; list-style-type: none; list-style-image: none;"><span style="font-family: "Microsoft YaHei";"><strong>2、匹配字符串中包含中文字符的正则表达式</strong></span></p><div class="codetitle"><span style="font-family: "Microsoft YaHei";">代码如下:</span></div><div id="code67037" class="codebody"><span style="font-family: "Microsoft YaHei";">"[\u4e00-\u9fa5]"</span></div><p style="margin: 10px auto; list-style-type: none; list-style-image: none;"><br><span style="font-family: "Microsoft YaHei";">说明:本例中只要求判断字符串中是否出现中文,所以不需要字符串头和尾,只要在整个字符串中有中文就可以被匹配到。</span></p><p style="margin: 10px auto; list-style-type: none; list-style-image: none;"><span style="font-family: "Microsoft YaHei";"> </span></p><p style="margin: 10px auto; list-style-type: none; list-style-image: none;"><span style="font-family: "Microsoft YaHei";"><strong>3、匹配字符串中以中文字符开头的正则表达式</strong></span></p><div class="codetitle"><span style="font-family: "Microsoft YaHei";">代码如下:</span></div><div id="code45010" class="codebody"><span style="font-family: "Microsoft YaHei";">"^[\u4e00-\u9fa5]"</span></div><p style="margin: 10px auto; list-style-type: none; list-style-image: none;"><br><span style="font-family: "Microsoft YaHei";">说明:在正则表达式的最前面加一个“^”,再紧跟一个中文字符,即可匹配以中文字符开头的字符串。</span></p><p style="margin: 10px auto; list-style-type: none; list-style-image: none;"><span style="font-family: "Microsoft YaHei";"> </span></p><p style="margin: 10px auto; list-style-type: none; list-style-image: none;"><span style="font-family: "Microsoft YaHei";"><strong>4、匹配字符串中以中文字符结尾的正则表达式</strong></span></p><div class="codetitle"><span style="font-family: "Microsoft YaHei";">代码如下:</span></div><div id="code84531" class="codebody"><span style="font-family: "Microsoft YaHei";">"[\u4e00-\u9fa5]$"</span></div><p style="margin: 10px auto; list-style-type: none; list-style-image: none;"><br><span style="font-family: "Microsoft YaHei";">说明:在正则表达式的最后面加一个“$”,在此之前加一个中文字符,即可匹配以中文字符结尾的字符串。</span></p><p style="margin: 10px auto; list-style-type: none; list-style-image: none;"> </p><div class="cnblogs_code" style="background-color: rgb(245, 245, 245); border: 1px solid rgb(204, 204, 204); padding: 5px; overflow: auto; margin-top: 5px; margin-bottom: 5px; color: rgb(0, 0, 0); font-family: "Courier New" !important; font-size: 12px !important;"><div class="cnblogs_code_toolbar"><span class="cnblogs_code_copy" style="padding-right: 5px; line-height: 1.5 !important;"><a title="复制代码" style="color: rgb(51, 153, 255); border: none !important;"><img src="https://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="float: left; max-width: 650px; height: auto;"></a></span></div><pre style="white-space: pre-wrap; list-style-type: none; list-style-image: none; font-family: "Courier New" !important;"><span style="color: rgb(0, 128, 128); line-height: 1.5 !important;"> 1</span> <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">string</span>[] RegexNumber =<span style="line-height: 1.5 !important;"> {</span><span style="color: rgb(0, 128, 128); line-height: 1.5 !important;"> 2</span> <span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">@"</span><span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">\d+.</span><span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">"</span><span style="line-height: 1.5 !important;">,
</span><span style="color: rgb(0, 128, 128); line-height: 1.5 !important;"> 3</span> <span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">@"</span><span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">\(\d{4}.+</span><span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">"</span><span style="line-height: 1.5 !important;">,
</span><span style="color: rgb(0, 128, 128); line-height: 1.5 !important;"> 4</span> <span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">@"</span><span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">\(\?\)</span><span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">"</span><span style="line-height: 1.5 !important;">,
</span><span style="color: rgb(0, 128, 128); line-height: 1.5 !important;"> 5</span> <span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">@"</span><span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">[\u4e00-\u9fa5]+$</span><span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">"</span>
<span style="color: rgb(0, 128, 128); line-height: 1.5 !important;"> 6</span> <span style="line-height: 1.5 !important;"> };
</span><span style="color: rgb(0, 128, 128); line-height: 1.5 !important;"> 7</span> <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">for</span> (<span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">int</span> j = <span style="color: rgb(128, 0, 128); line-height: 1.5 !important;">0</span>; j < RegexNumber.Length; j++<span style="line-height: 1.5 !important;">)
</span><span style="color: rgb(0, 128, 128); line-height: 1.5 !important;"> 8</span> <span style="line-height: 1.5 !important;"> {
</span><span style="color: rgb(0, 128, 128); line-height: 1.5 !important;"> 9</span> <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">if</span><span style="line-height: 1.5 !important;"> (Regex.IsMatch(name, RegexNumber, RegexOptions.IgnoreCase))
</span><span style="color: rgb(0, 128, 128); line-height: 1.5 !important;">10</span> <span style="line-height: 1.5 !important;"> {
</span><span style="color: rgb(0, 128, 128); line-height: 1.5 !important;">11</span> Match match =<span style="line-height: 1.5 !important;"> Regex.Match(name, RegexNumber, RegexOptions.IgnoreCase);
</span><span style="color: rgb(0, 128, 128); line-height: 1.5 !important;">12</span> Name = name.Replace(match.Value, <span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">""</span><span style="line-height: 1.5 !important;">);
</span><span style="color: rgb(0, 128, 128); line-height: 1.5 !important;">13</span> name =<span style="line-height: 1.5 !important;"> Name;
</span><span style="color: rgb(0, 128, 128); line-height: 1.5 !important;">14</span> <span style="line-height: 1.5 !important;"> }
</span><span style="color: rgb(0, 128, 128); line-height: 1.5 !important;">15</span> }</pre><div><br></div><div class="cnblogs_code_toolbar"><span class="cnblogs_code_copy" style="padding-right: 5px; line-height: 1.5 !important;"><a title="复制代码" style="color: rgb(51, 153, 255); border: none !important;"></a></span></div></div></div>
页:
[1]