[英]Is there a lowercase expression for .Net regular expressions?
Perl has the \\u operator to lowercase a match when using string replacement and regular expressions. 在使用字符串替换和正则表达式时,Perl具有\\ u运算符以小写匹配。 Does .Net have anything similar? .Net有类似的东西吗? For example, uppercase all words that start with a < 例如,大写所有以<开头的单词
s/<\(\w*\)/<\U\1/
The way to do these kind of things in .NET is using the MatchEvaluator parameter: 在.NET中执行这些操作的方法是使用MatchEvaluator参数:
string pattern = @"<(\w*)";
string replaced = Regex.Replace(line, pattern,
x => "<" + x.Groups[1].ToString().ToUpper());
This reads: Whenever you find the regular expression, replace it with the first group uppercased. 其内容如下:每当您找到正则表达式时,请将其替换为第一个大写的组。
You've got some errors in your Perl code. 你的Perl代码中有一些错误。 In both Perl and .NET regexes, \\(
and \\)
match the literal characters, (
and )
; 在Perl和.NET正则表达式中, \\(
和\\)
匹配文字字符, (
和)
; to use parentheses as grouping operators, leave the backslashes off. 要使用括号作为分组运算符,请保留反斜杠。 Also, \\u\u003c/code> does not lowercase a match , it titlecases (usually the same as uppercasing) the next character .
此外,
\\u\u003c/code>不会小写匹配 ,它会标记下一个字符 (通常与大写相同)。
What you're thinking of is
\\L
, which lowercases all characters until the end of the string or \\E
, whichever comes first. 你想到的是\\L
,它将所有字符小写到字符串的末尾或\\E
,以先到者为准。
In Perl,
\\U
, \\L
and such aren't really a regex feature, they're a string feature, like the more common escape sequences: \\n
, \\t
, etc.. They're listed redundantly in the regex docs because they're especially useful in regex substitutions. 在Perl中, \\U
, \\L
等并不是真正的正则表达式功能,它们是一个字符串功能,就像更常见的转义序列: \\n
, \\t
等等。它们在正则表达式文档中被冗余列出因为它们在正则表达式替换中特别有用。 C# has no equivalent for them, either in string literals or the regex classes, but as @steinar pointed out, it does have MatchEvaluator and (since .NET 3.0) lambda expressions: C#在字符串文字或正则表达式类中没有它们的等价物,但正如@steinar指出的那样,它确实有MatchEvaluator和(自.NET 3.0以来)lambda表达式:
string s = "ABC<XYZ!";
Console.WriteLine(Regex.Replace(s, @"<(\w+)", m => m.Value.ToLower()));
output: 输出:
ABC<xyz!
edit: The parentheses aren't really necessary in my example, but I left them in to demonstrate their proper use as grouping operators. 编辑:在我的示例中,括号并不是必需的,但是我将它们留下来展示它们作为分组运算符的正确用法。 I also changed the original
\\w*
to \\w+
; 我也将原来的\\w*
改为\\w+
; there's no point matching zero word characters when your only goal is to change the case of word characters. 当你唯一的目标是改变单词字符的情况时,没有必要匹配零字符。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.