Click here to Skip to main content
14,828,863 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Ok i have the following code, but when it runs it doesn't return the replaced string;

C#
private string ReplaceDescription(string description)
       {
           var newDescription = description.Replace("/en/products/purpose/", "/en/");
           return newDescription;
       }

       private string ReplaceProductDescription(string description)
       {
           string pattern = @"/en/products/\d{4}/\d{4}\.aspx";
           Regex regEx = new Regex(pattern);
           foreach (Match match in regEx.Matches(description))
           {
               string matchedValue = match.Value;
               string productNumber = System.IO.Path.GetFileNameWithoutExtension(matchedValue);
               description.Replace(matchedValue, "newString");
           }
           return description;

       }


The first method works fine, but the second one wont return the description with replacements, any help would be great many thanks.
Posted

Why not just have the Regex do the replacement?
C#
private string ReplaceProductDescription(string description)
{
    string pattern = @"/en/products/(?<pn1>\d{4})/(?<pn2>\d{4})\.aspx";
    Regex regEx = new Regex(pattern);
    return regEx.Replace(description, "newString pn1 here:${pn1} pn2 here:${pn2}");
}

And you probably want to pre-construct the Regex once, instead of on each call.
   
v3
Comments
Grant Weatherston 21-Nov-14 13:19pm
   
I would but I need to access the match in order to retrieve the product number as the product number is in the new string .
Matt T Heffron 21-Nov-14 14:13pm
   
Well you didn't say that in the first place...
So you can use captured patterns in the substitution string.
I've updated my solution.
You should read up on all of the capabilities of the Regex.Replace() !
Of course, you can arrange the capture(s) of the pattern however you need. You don't need to split it into two parts.
Matt T Heffron 21-Nov-14 21:33pm
   
How's this working for you?
Please "Accept" this Solution (if appropriate).
Maciej Los 21-Nov-14 14:53pm
   
+5!
Matt T Heffron 21-Nov-14 16:36pm
   
Thanks
The Replace method does not change the original string. This means that the Replace method has no effect in your second method.

Try this instead:
C#
private string ReplaceProductDescription(string description)
{
    string pattern = @"/en/products/\d{4}/\d{4}\.aspx";
    Regex regEx = new Regex(pattern);
    string updatedDescription = description;
    foreach (Match match in regEx.Matches(description))
    {
        string matchedValue = match.Value;
        string productNumber = System.IO.Path.GetFileNameWithoutExtension(matchedValue);
        updatedDescription = updatedDescription.Replace(matchedValue, "newString");
    }
    return updatedDescription;
}
   
v2
Comments
Grant Weatherston 21-Nov-14 13:05pm
   
Would that work though as updated would get overwritten each loop when a new match is iterated, as updated = current description. Replace it would t have all replacements done.
Thomas Daniels 21-Nov-14 13:21pm
   
You're right. I will edit my answer.
Thomas Daniels 21-Nov-14 13:24pm
   
I edited my answer. Now the value of updatedDescription is the same as description before the loop starts, and the value of updatedDescription gets always changed inside the loop.
Maciej Los 21-Nov-14 14:51pm
   
As many matches as many times updatedDescription is created. String is immutable!
http://www.codeproject.com/Articles/406046/Why-strings-are-immutable-and-what-are-the-implica
Thomas Daniels 22-Nov-14 8:53am
   
I know. It's true that the string gets recreated many times, but it does what the OP wants.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900