Hi,
I need a solution for the following scenario.
I have two urls like:
string browserURL= "/spm/kpi-setup/view/{spmkpiinstance}/preventative-analysis";
string tobeInjecetedURL = "/spm/kpi-setup/view/40526/corrective-actions/";
My requirement:
Need to check if the browserURL is equivalent with tobeInjecetedURL (highlighted in yellow).
i.e : Need to check if the url matches just before the curly braces.
if it matches then I need to replace the {spmkpiinstance} with value 40526.
Please suggest a generic solution for this .
4 Replies
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.

VulpesPosted Aug 19, 2014, 2:39 PM
For example in the second case, how do you know you're trying to match '/spm/kpi-setup/view/' and not '/spm/kpi-setup/' or '/spm/kpi-setup/view/preventative-analysis/' ?
Also, if it's not adjacent to what you're trying to match, how do you know what the replacement is? For example in the third case, how do you know it's '40526' and not 'corrective-actions' ?
Praveen Raveendran PillaiPosted Aug 19, 2014, 12:17 PM
Need to handle many cases.
Case 1:
Case 2:
Case 3:
VulpesPosted Aug 19, 2014, 7:52 AM
The output now is:
VulpesPosted Aug 19, 2014, 7:37 AM
using System;
using System.Text.RegularExpressions;
class Test
{
static void Main()
{
string browserURL= "/spm/kpi-setup/view/{spmkpiinstance}/preventative-analysis";
string tobeInjectedURL = "/spm/kpi-setup/view/40526/corrective-actions/";
URLReplace(ref browserURL, tobeInjectedURL);
Console.WriteLine(browserURL);
Console.ReadKey();
}
static void URLReplace(ref string browserURL, string tobeInjectedURL)
{
string regExp = @"^(.*?)(\{.*?})(.*)$";
Match m = Regex.Match(browserURL, regExp);
string start = m.Groups[1].Value;
if (tobeInjectedURL.StartsWith(start))
{
int len = start.Length;
int index = tobeInjectedURL.IndexOf('/', len);
string replacement = tobeInjectedURL.Substring(len, index - len);
browserURL = start + replacement + m.Groups[3].Value;
}
}
}
The output should be:
/spm/kpi-setup/view/40526/preventative-analysis