开发者

Replace any character in between AnyText: and <usernameredacted@example.com> with an empty string using Regex?

开发者 https://www.devze.com 2023-03-16 15:01 出处:网络
I have this string AnyText: \"TomDickHarry\" <usernameredacted@example.com> Desired Output Using Regex

I have this string

AnyText: "TomDickHarry" <usernameredacted@example.com>

Desired Output Using Regex

AnyText: <usernameredacted@example.com>

Help to replace anything in between AnyText: and <usernameredacted@example.com> with an empty string using Regex.

I am still a rookie at regular expressions. Could anyone out there help me with the matching & repla开发者_JAVA百科cing expression for the above scenario?


string ABC = "AnyText: \"TomDickHarry\" <usernameredacted@example.com>"
Regex RemoveName = new Regex("(?<=AnyText:).*(?=<)");
string XYZ = RemoveName.Replace(ABC, "");

So, this will find a regex match in the string you supplied, and on the third line, replace it with an empty string.


const string Input = @"AnyText: ""TomDickHarry"" <usernameredacted@example.com>This is.";

var result = Regex.Replace(Input, "(?<=AnyText:)([^<]*)", string.Empty);


This works for me:

string s = Regex.Replace(Input, ":(.*)<", "");
0

精彩评论

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