0

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 & replacing expression for the above scenario?

Alan Moore
  • 73,866
  • 12
  • 100
  • 156
Derek
  • 2,185
  • 4
  • 20
  • 24
  • possible duplicate of [Replace any character before with an empty string](http://stackoverflow.com/questions/6529471/replace-any-character-before-usernameredactedexample-com-with-an-empty-string) – Asaph Jun 30 '11 at 05:04
  • This is almost the same as your question from 2 hours ago. – Asaph Jun 30 '11 at 05:05
  • What additional changes? – Asaph Jun 30 '11 at 05:06
  • The Desired Output is different. Any character in between must be replaced with an empty string – Derek Jun 30 '11 at 05:07

3 Answers3

1
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.

Paul McLean
  • 3,450
  • 6
  • 26
  • 36
0
const string Input = @"AnyText: ""TomDickHarry"" <usernameredacted@example.com>This is.";

var result = Regex.Replace(Input, "(?<=AnyText:)([^<]*)", string.Empty);
Kirill Polishchuk
  • 54,804
  • 11
  • 122
  • 125
0

This works for me:

string s = Regex.Replace(Input, ":(.*)<", "");
Emiliano Poggi
  • 24,390
  • 8
  • 55
  • 67