Solved

regular expression to replace Month-YY with YYYYMM

Posted on 2008-10-29
6
539 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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

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…
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…

791 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