从一段字符串中,提取中文、英文、数字
中文字符30Margin中文字符40HorizontalAlignment
正则表达式:
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:使用正则匹配字符内容,不能使用开始、结束字符( ^文本开始; $文本结束)。
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}";
注:
Regex.Match只会返回第一个匹配项
如果需要获取正则对应的所有匹配项,可以使用 Regex.Matches
作者:唐宋元明清2188