Solved

regular expression to replace Month-YY with YYYYMM

Posted on 2008-10-29
6
535 Views
Last Modified: 2012-06-27
Can someone give me a regular expression that will match a list of reports named like this:
MFR-Apr06
MFR-Apr07
MFR-Apr08
MFR-Aug06
MFR-Aug07
MFR-Aug08
MFR-Dec06
MFR-Dec07
MFR-Dec08

To names like this:
MFR-200604
MFR-200608
MFR-200612
MFR-200704
MFR-200708
MFR-200712
MFR-200804
MFR-200808
MFR-200812

Thanks
0
Comment
Question by:megnin
  • 3
  • 2
6 Comments
 

Expert Comment

by:Fadi_Mansour
ID: 22835381
What do you mean by "match", do you mean to change (as in search-and-replace) or do you mean that they should be considered identical in a comparison context?
0
 
LVL 1

Author Comment

by:megnin
ID: 22835418
Oh, sorry.  Yeah that was a bit confusing.  Search and replace.  I have a folder full of files named like the former format and I need to rename them all like the latter.
I have a "batch rename" utility that accepts regular expressions to actually rename the files, but comming up with the regular expression is my challange.
Thanks.
0
 
LVL 5

Accepted Solution

by:
bitter_chicken earned 500 total points
ID: 22835556
I suggest doing this in two steps:

1)

use this search/replace regex:

search:
MFR-(\w{3})(\d{2})

replace:
MFR-20\2\1

step one will change the format from
MFR-Apr06 to MFR-2006Apr

2)

search/replace each month abbreviation separately... for each month use this regex:

search (change the month abbrev and run once for each month):

MFR-(\d{4})Apr

replace (change the 04 to the appropriate month number):

MFR-\0104
0
ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

 
LVL 5

Expert Comment

by:bitter_chicken
ID: 22835573
nb.

you might need to change step 2) replace code to
MFR-\104

if your regex flavour doesn't support more than 9 backreferences
0
 
LVL 1

Author Comment

by:megnin
ID: 22835880
Thank you.  Just what I needed.  I don't use regex enough to get the hang of backreferences.
0
 
LVL 1

Author Closing Comment

by:megnin
ID: 31511378
Thanks again!!!
0

Featured Post

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have been reconstructing a PHP-based application that has grown into a full blown interface system over the last ten years by a developer that has now gone into business for himself building websites. I am not incredibly fond of writing PHP code o…
As most anyone who uses or has come across them can attest to, regular expressions (regex) are a complicated bit of magic. Packed so succinctly within their cryptic syntax lies a great deal of power. It's not the "take over the world" kind of power,…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question