开发者

How do I match the last character in an arbitrary string using C# Regexes?

The obvious attempt is:

Regex.Replace(input, @".$", "X", RegexOptions.Singleline);

This doesn't always work though. Consider the string \r\n\r\n - the above produces the surprising result of \r\nXX. One might expect from reading MSDN (under Multiline) that $ should match just at the end of the entire string, but apparently $ actually means "match at end of string or at the \n just before the end of string".

What might be a correct way to match the last character of an arbitrary stri开发者_如何学Cng?


.NET supports the \z token, which always matches the end of the string:

Regex.Replace(input, @".\z", "X", RegexOptions.Singleline);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜