开发者

Regex .NET question

开发者 https://www.devze.com 2023-01-07 21:00 出处:网络
Is it possible using Regex.Replace to match a string, but only replace a portion of that matched string?Some way to mark part of the string that should be replaced with the replacement text parame开发

Is it possible using Regex.Replace to match a string, but only replace a portion of that matched string? Some way to mark part of the string that should be replaced with the replacement text parame开发者_高级运维ter?


You can use groups to insert parts of the original string, or you can use lookbehind and lookahead.

Examples

Using Groups:

someString = Regex.Replace(someString, @"(before)content(after)", "$1new content$2");

Using lookaround:

someString = Regex.Replace(someString, @"(?<=before)content(?=after)", @"new content");
0

精彩评论

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