从一段字符串中,提取中文、英文、数字正则表达式
中文字符30Margin中文字符40HorizontalAlignmentspa
正则表达式:code
1 /// <summary> 2 /// 英文字母与数字 3 /// </summary> 4 public const string LettersAndNumbers = "[a-zA-Z0-9]+"; 5 6 /// <summary> 7 /// 中文字符 8 /// </summary> 9 public const string ChineseChars = "[\u4E00-\u9FA5]+"; 10 11 /// <summary> 12 /// 英文字符 13 /// </summary> 14 public const string EnglishChars = "[a-zA-Z]+";
PS:使用正则匹配字符内容,不能使用开始、结束字符( ^文本开始; $文本结束)。blog
Regex使用:字符串
1 string ChineseChars = "[\u4E00-\u9FA5]+"; 2 var match = Regex.Match("中文字符30Margin中文字符40HorizontalAlignment", ChineseChars, RegexOptions.IgnoreCase); 3 var result = $"Index:{match.Index},Length:{match.Length}\r\nResult:{match.Value}";
注:string
Regex.Match只会返回第一个匹配项io
若是须要获取正则对应的全部匹配项,可使用 Regex.Matchesclass