<分区>
我需要找到字符串
中的所有大写字母。例如
输入:电子与通信工程
输出:ECE
<分区>
我需要找到字符串
中的所有大写字母。例如
输入:电子与通信工程
输出:ECE
最佳答案
如果您坚持使用正则表达式:
string source = @"Electronics and Communication Engineering";
string result = string.Concat(Regex
.Matches(source, "[A-Z]")
.OfType<Match>()
.Select(match => match.Value));
Linq 是一个(更短的)替代方案:
string result = string.Concat(source.Where(c => c >= 'A' && c <= 'Z'));
编辑:如果“大写字母”包括所有 Unicode 大写字母,不仅是英语,还有俄语字母,正则表达式将使用不同的模式
string result = string.Concat(Regex
.Matches(source, @"\p{Lu}")
.OfType<Match>()
.Select(match => match.Value));
和Linq解决方案将使用不同的条件:
string result = string.Concat(source.Where(c => char.IsUpper(c)));
关于c# - 查找字符串中的所有大写字母 - 正则表达式 C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44363153/