1

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.

4
  • What does ""{0,1} mean at the end of the pattern? Commented Dec 16, 2013 at 16:17
  • 1
    Your regex doesn't match the first string - \w doesn't include the - character. Running the code on the second string produces the expected result: /media/1048/NEWFILENAME.jpg Commented Dec 16, 2013 at 16:40
  • The ""{0,1} was left over from when I thought I might need it to resolve a filename within a href-tag. Commented Dec 16, 2013 at 20:21
  • The expected result would have been for example /media/1047/anewfilenamesuppliedinthemethod.jpg, the examples where just picked to illustrate instances where it did not work. Commented Dec 17, 2013 at 9:03

1 Answer 1

2

Not sure what exactly some of the objects in your expression are supposed to accomplish, but assuming you simply want to capture the path, filename and extension separately, this cleaned up expression should work for you:

(\/media\/\d+\/)([\w-]+)(\.(?:png|gif|jpe?g|pdf|bmp))

It now matches - in the filename, jpg or jpeg, and I removed the ""{0,1} from the end.

On both of your content examples:

MATCH 1

  1. /media/1048/
  2. 300x300-tK3s__MG_4391
  3. .jpg

MATCH 2

  1. /media/1047/
  2. 300X300tk3s_M1Y9216
  3. .jpg

Working example: http://regex101.com/r/jR1cX1 (note the escaped slashes as well -- they shouldn't affect C#, but just in case...)

Sign up to request clarification or add additional context in comments.

3 Comments

Hmm, strike that, I still have the same problem, the replace method doesn't do the same thing as the match so while using the Groups property works - and always did - the Replace method does things differently.
Alright, can you elaborate in the question? Or did you already? Also see this - might be the cause: stackoverflow.com/questions/8432983/…
Yes, this is the issue I'm seeing. After wrapping the back-reference in curly braces, eg, r.Replace(content, "${1}" + newname + "${3}") I get the expected result.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.