Reputation: 762
I have some "paths"
that are in a format similar to:
/Media/Default/images/Tulips.jpg
And I have the ability to get the "root path" which will return something similar to either:
http://example.com/Media/Default/
/another-prefix/Media/Default/
what I need to do is match the start of the "path" string with the end of "root path" string to return images/Tulips.jpg
What i really need is a generic regular expression that will compare pare the 2 strings
do a sort
of an overlay and trim at the greatest possible match. That way it will work if the inputs were changed to:
Path: qwerty-some-random-string
Root Path: junk-qwerty
Result: -some-random-string
Upvotes: 1
Views: 161
Reputation: 437386
Tries all suffixes of root
in descending order of length and stops when it determines that one of them is a prefix of path
:
var path = "qwerty-qwerty-some-random-string";
var root = "aardvark-junk-qwerty-qwerty";
var prefix = root.Select((ch, i) => root.Substring(i))
.Where(pref => path.StartsWith(pref))
.DefaultIfEmpty("")
.First();
var result = path.Substring(prefix.Length);
Note: Initially I posted a regex solution here but it turned out that it was completely unworkable.
Upvotes: 2