Regex to replace everything not in between two characters in a string?

Viewed 34

Suppose I have a string:

Successfully made resource ->resourceId<- in the system.

How can I do a regex replacement so that I can end up with only

resourceId 

Having trouble implementing this.

2 Answers

You can use named capture groups in Regex to get it:

var regex = new Regex(@"\>(?<ResourceId>\w+?)\<");
var str = "Successfully made resource ->resourceId<- in the system.";
var resourceId = regex.Match(str).Groups["ResourceId"];

Play with the Fiddle here.

This is kind of wonky but would do the trick...

var splitList = txtString.Split('-').ToList();

splitList.ForEach(r => r.Replace('<', ' ')); splitList.ForEach(r => r.Replace('>', ' '));

splitList.ForEach(r => r = r.Trim());

Related