从一段字符串中,提取中文、英文、数字

中文字符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