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 .

VulpesRank #55 · 29.2K reputationPosted Aug 19, 2014, 3:37 AM EDT
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
VulpesRank #55 · 29.2K reputationPosted Aug 19, 2014, 3:52 AM EDT
The output now is:
Praveen Raveendran PillaiRank #694 · 3.5K reputationPosted Aug 19, 2014, 8:17 AM EDT
Need to handle many cases.
Case 1:
Case 2:
Case 3:
VulpesRank #55 · 29.2K reputationPosted Aug 19, 2014, 10:39 AM EDT
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' ?
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questionsTop experts