Solved

regular expression to replace Month-YY with YYYYMM

Posted on 2008-10-29
6
527 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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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…
Whatever be the reason, if you are working on web development side,  you will need day-today validation codes like email validation, date validation , IP address validation, phone validation on any of the edit page or say at the time of registration…
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…

746 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now