.NET RegEx 可以在没有 .ToUpper() 的情况下将字母大写吗?
Can .NET RegEx capitalize letters without .ToUpper()?
我正在尝试通过 .NET 正则表达式来使用 ADFS Claims Rule Language for a simple task: capitalize some text. The language does not have common string manipulation methods like .ToUpper()
, but it does have a Regex.Replace 宏。
遗憾的是,.NET 正则表达式不支持 Perl's \U
operator,后者可以很好地解决问题(例如 s/[a-z]/\U/g
)。
有没有什么方法可以让普通的 Regex.Replace(string, string, string)
命令将字母 大写而不使用 使用 .ToUpper()
等?
我不知道在 .NET 的表达式中有什么简单的方法可以做到这一点,但是您可以在 Regex.Replace() 的调用中使用 MatchEvaluator lambda,类似于此处描述的内容:
How to uppercase the first character of each word using a regex in VB.NET?
正如 Colin 所说,MatchEvaluator 是您的最佳选择。您可以这样做来将语句的第一个字母大写:
var s = System.Text.RegularExpressions.Regex.Replace
("capitalise the first letter of this sentence.", "(.*)",
delegate(System.Text.RegularExpressions.Match m) {
return (m.Value.Length > 0 ? m.Value.Substring
(0, 1).ToUpper() : "") + (m.Value.Length > 1 ? m.Value
.Substring(1, m.Value.Length-1) : "");
});
否
参见Substitutions in Regular Expressions。仅支持以下替换元素:$number
、${name}
、$$
、$&
、$`
、$'
、$+
、 $_
。您不能转换元素、使用条件或类似的东西。
我正在尝试通过 .NET 正则表达式来使用 ADFS Claims Rule Language for a simple task: capitalize some text. The language does not have common string manipulation methods like .ToUpper()
, but it does have a Regex.Replace 宏。
遗憾的是,.NET 正则表达式不支持 Perl's \U
operator,后者可以很好地解决问题(例如 s/[a-z]/\U/g
)。
有没有什么方法可以让普通的 Regex.Replace(string, string, string)
命令将字母 大写而不使用 使用 .ToUpper()
等?
我不知道在 .NET 的表达式中有什么简单的方法可以做到这一点,但是您可以在 Regex.Replace() 的调用中使用 MatchEvaluator lambda,类似于此处描述的内容:
How to uppercase the first character of each word using a regex in VB.NET?
正如 Colin 所说,MatchEvaluator 是您的最佳选择。您可以这样做来将语句的第一个字母大写:
var s = System.Text.RegularExpressions.Regex.Replace
("capitalise the first letter of this sentence.", "(.*)",
delegate(System.Text.RegularExpressions.Match m) {
return (m.Value.Length > 0 ? m.Value.Substring
(0, 1).ToUpper() : "") + (m.Value.Length > 1 ? m.Value
.Substring(1, m.Value.Length-1) : "");
});
否
参见Substitutions in Regular Expressions。仅支持以下替换元素:$number
、${name}
、$$
、$&
、$`
、$'
、$+
、 $_
。您不能转换元素、使用条件或类似的东西。