I need help making a regular expression. I have a string that is known at compile time, let's call it
Show to be a heartbreakerpecificString
how to be a heartbreaker
SpecificSt this is the last example ring
this is the last example
static string GetArbitraryString(string input)
const string specificString = "SpecificString";
var regex = // regex pattern to find
var match = regex.Match(input);
string arbitraryString = match.CapturedGroups.Value;
Well, here's the best I've got in terms of a regex answer (though it's still pretty damn inelegant in my opinion):
Then, all you have to do is iterate over the capture groups and pick up the one that isn't empty. Simple as that.
And, since you're in C#, you can even use named capture groups with the same name for all of them. Whichever one gets picked up will be the value of the named capture.