Got more questions? Find advice on: ASP | SQL | XML | Windows
in Search
Welcome to RegexAdvice Sign in | Join | Help

extracting two ends of a filename & concatenating

Last post 11-17-2009, 5:52 PM by Keith Lutz. 2 replies.
Sort Posts: Previous Next
  •  11-17-2009, 2:20 PM 57371

    extracting two ends of a filename & concatenating

    First let me say, I am not familar with Regular expressions, but would love to learn and this site seems really cool.

    Second, is my problem. 

    I am passing in a file name and currently doing this in Perl.

         $fileName1 = shift;
         #print "$fileName1";
         $fileName1 =~ s/\.met|\.ps/\.ps/gi;
         return($fileName1);

     What does this do?

    Here is my real problem.

    If I needed to pass in a file name like this... TEST_ClientName_mst_43a_a1_VSMST43W_PSFILE_D111709T132200.ps but needed the fullname without the _PSFILE... so it would look like this after -  TEST_ClientName_mst_43a_a1_VSMST43W_D111709T132200.ps

    How would I accomplish that?  Of course the date/time stamp is changing from run to run as is the "W",  the underscores are always the same amount going in.

    Thank you in advance!

  •  11-17-2009, 5:18 PM 57373 in reply to 57371

    Re: extracting two ends of a filename & concatenating

    The pattern in your sample code scans whatever text is in the $fileName1 variable for either of the character sequences ".met" or ".ps" and replaces them with ".ps". It does this for all occurrences within the string (the 'g' option at the end of the pattern) and regardless of upper/lower case (the 'i' option).

    One of the options that is searched for is ".ps" which is the same as the replacement string. While this is not wrong (and can be a useful 'trick' if there are other things happening such as delegate functions [or whatever they are called in Perl] being called etc.) you can make the line just:

    $fileName1 =~ s/\.met/\.ps/gi;

    This will substitute all occurrences of ".met" with ".ps".

    As for your last question, if you are only passing the filename (and nothing else) then a pattern of:

    ^(.*?)_PSFILE_(.*)$

    and a replacement string of

    $1_$2

    possibly with the 'ignore case' option set, will result in the effective removal of the "_PSFILE_" text from the string. It works by capturing all of the text from the start to just before the "_PSFILE__" text in match group #1, and all of the text from after the "_PSFILE_" to the end of the string in match group #2. It then joins the two captured parts together with an underscore between them. Note that, even if the "global" option is give, this will only replace the first occurrence of "_PSFILE_" in the string.

    There is an alternative which is the use the pattern:

    _PSFILE_

    and a replacement string of

    _

    This locates the "_PSFILE_" text and replaces it with an underscore. This doesn't care too much about the rest of the string, and will replace all instances of "_PSFILE_" but will do so in text such as:

    This pattern will remove _PSFILE_ text character from a string

    resulting in "This pattern will remove _ text character from a string".

    As always there are many ways to solve the problem, and usually the "right way" depends on the broader context of your work.

    Susan

  •  11-17-2009, 5:52 PM 57376 in reply to 57373

    Re: extracting two ends of a filename & concatenating

    Thank you soooo-much Susan, that has helped immensely!
View as RSS news feed in XML