c#中的正则使用
<span style="font-size:18px;">using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
public static void Main(string[] args)
{
string pattern = @"\b(\w+)\s\1\b";//匹配重复单词.
Regex rgx = new Regex(pattern, RegexOptions.IgnoreCase);
string str = "ab ab ed ed adf 12 12";//匹配的字符串
MatchCollection matches = rgx.Matches(str);
foreach (Match ma in matches)
{
Console.WriteLine(" " + ma.Value);
}
}</span>
常用正则:
身份证号:
Regex reg = new Regex(@"(^\d{15}$)|(^\d{17}[\dxX]$)");正数可有小数点:
^((?!0\d)\d+(\.\d+)?\*){2}?(?!0\d)\d+(\.\d+)?$
^(0|([1-9]\d*))(\.\d+)?$
固定电话号码匹配.以0开头,后面有2-3个数字,然后是一个连字号"-",最后是7或8位数字.(010-12345678或0376-7654321)
0\d{2,3}-\d{7,8}
\S+匹配不包含空白符的字符串。
[\s\S]* 匹配任意字符(包括换行符)
<a[^>]+>匹配用尖括号括起来的以a开头的字符串。
\ba\w*\b匹配以字母a开头的单词.
\d+匹配1个或更多连续的数字。
\b\w{6}\b 匹配刚好6个字母/数字的单词。
(\d{1,3}\.){3}\d{1,3}是一个简单的IP地址匹配表达式。
((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)一个IP地址,每一个数都不超过255.
\b(\w+)\s\1\b匹配重复的单词.
^[\u4E00-\u9FA5]+$ 匹配汉字