I have a code snippet that runs on .Net 4.5 and looks roughly like this:
function string replace(string content, string newfilename ) {
Regex r = new Regex(@".*(/media/\d+/)(\w+)(\.(?:png|gif|jp{0,1}g|pdf|bmp))""{0,1}");
var str1 = string.Empty;
var str2 = string.Empty;
var m = r.Matches(content);
if (m.Count > 0)
{
str1 = r.Replace(content, "$1" + newfilename + "$3");
str2 = m[0].Groups[1].Value + newfilename + m[0].Groups[3].Value;
}
...
And given the input (one call per string, newname being the same as regex match group 2)
content #1:
/media/1048/300x300-tK3s__MG_4391.jpg
str1:
/media/1048/300x300-tK3s__MG_4391.jpg
str2:
/media/1048/300x300-tK3s__MG_4391.jpg
content #2:
/media/1047/300X300tk3s_M1Y9216.jpg
str1:
$1300X300tk3s_M1Y9216.jpg
str2:
/media/1047/300x300tK3s__M1Y9216.jpg
What's going on in the r.Replace() statement for the second string? Why do I get $1 newfilename $3? The only difference in this specific example is the dash in the string that works and the lack of a dash in the string that doesn't.
(Note that the input string might be preceded with html markup, hence the .* in the beginning of the regex)
EDIT What I am trying to accomplish is to rename a set of files in a collection of textual references to them with a basic format of /media/(int)/(filename).(extension) so these examples where not really ideal. Given the second example with the newfilename parameter with the value of MynewImage , the result would have been $1MynewImage.jpg where I would have expected it to be /media/1047/MynewImage.jpg.
And the dash not being matched by \w is not correct it seems, it DOES match the dash and when there IS a dash, the replace method works, it's when there isn't a dash that it doesn't and returns the grouping parameter literally instead of the actual value of the first capture group. But not for ALL filenames with dashes, just these ones.
""{0,1}mean at the end of the pattern?\wdoesn't include the-character. Running the code on the second string produces the expected result:/media/1048/NEWFILENAME.jpg