Share via


Regex 101 Exercise S6 - Change the extension on a file

Regex 101 Exercise S6 - Change the extension on a file

Given a filename including path, change the extension to .out.

Input string example: 

C:\utility\Processor.cs

Notes:

  1. The best answer to this is really to use System.IO.Path.ChangeExtension(), but that wouldn't be much of a Regex exercise, now would it?
  2. It's not as simple as it looks

Comments

  • Anonymous
    November 28, 2005
    Regex.Replace(@"C:utilityProcessor.cs", @"[^.]+.([^.]+)$", "vb")

  • Anonymous
    November 28, 2005
    whoops! forgot to include the path

    Regex.Replace(@"C:utilityProcessor.cs", @"(?<path>[^.]+.)[^.]+$", @"${path}vb")

  • Anonymous
    November 28, 2005
    and apparently I can't read.

    Regex.Replace(@"C:utilityProcessor.cs", @"(?<path>[^.]+.)[^.]+$", @"${path}out")

  • Anonymous
    November 28, 2005
    I like:
    Regex.Replace(filename, @".(.*?)$", "out");

  • Anonymous
    November 28, 2005
    Regex.Replace(input,"(?<file>.+[.]+)+(?<ext>.+)+","${file}out");

  • Anonymous
    November 28, 2005
    slightly trimmed down version of the above regex:
    Regex.Replace(input,"(?<file>.+[.]+)(.+)","${file}out");

    input: c:xyzabc.exe.config
    output: c:xyzabc.exe.out

    yippie!

  • Anonymous
    November 28, 2005
    I assume that the regex should work regardless of the number of .s in the path. No problem there.

    But... can I assume that there is at least one dot in the filename proper? (not including the path.)

    Can I also assume that the last character is not a dot?

    If so...

    string DotOutIfy(string sFileName)
    {
    __ // easy to read
    __ return Regex.Replace(sFileName, ".(.+)", ".out", RegexOptions.RightToLeft);

    __ // perhaps more accurate
    __ return Regex.Replace(sFileName, ".([^.]+)$", ".out");

    }

    If I can't assume those things, I should check for them and raise an appropriate error.

  • Anonymous
    November 28, 2005
    What is "expected behavior" if there is a FOLDER named
    C:utilityProcessor.out

    ?

    I assume that this is the caller's problem, not the DotOutIfy function's problem...

  • Anonymous
    November 28, 2005
    (@".([^.]+)$, ".out")

  • Anonymous
    November 29, 2005
    Hmmm... it seems that Regex.Replace(string, string, string, regexoptions) will check the entire string for all occurrences of the pattern (in a "s///g" sense, not a "1 while s///" sense) so my initial submission is wrong.

    There's a Regex.Replace signature that allows specifying a maximum number of replacements... but no signature that allows BOTH specifying a number of replacements AND RegexOptions.RightToLeft...

    It's beginning to seem to me that perhaps .NET regexes are not suited to this.

    If this was perl it would be easy:

    $fileName =~ s/.([^.]+)$/.out/; # no /g

    But there's no simple .NET regex equivalent.

    Perhaps it would be better to find the index of the last . in the string (n), take the left (n - 1) characters of the string, and add ".out"

  • Anonymous
    December 01, 2005
    Replace( filename, @".(^.)*$", "out" );

  • Anonymous
    December 05, 2005
    oops, it should be ".[^.]*$"
    ( next time i'll actually test the regex before posting :) )

  • Anonymous
    June 07, 2009
    PingBack from http://greenteafatburner.info/story.php?id=2275

  • Anonymous
    June 17, 2009
    PingBack from http://pooltoysite.info/story.php?id=3268