I am trying to replace a URL query pattern wID=xxx
, where xxx
can be a digit, number or white space, with the query wID=[[WID]]
(note that the query is case sensitive). I would like to know how can I achieve this. Currently, I am using regex to find the pattern in the URL and replace it using Regex.Replace()
as follows:
private const string Pattern = @"wID=^[0-9A-Za-z ]+$";
private const string Replace = @"wID=[[WID]]";
/// <summary>
/// Extension method to evaluate the url token for wid
/// </summary>
/// <param name="rawUrl">Incoming URL</param>
/// <returns>Expected URL</returns>
public string GetUrl(string rawUrl)
{
if (string.IsNullOrWhiteSpace(rawUrl))
{
return string.Empty;
}
string result = Regex.Replace(rawUrl, Pattern, Replace);
return result;
}
But this isn't giving me the desired output, as the regex pattern is incorrect, I suppose. Any better way to do this?
My question was related to the implementation of regex pattern to find and replace the URL query parameter value, I found the URI builder is more helpful in these cases and can be used rather so it's different question.