Solved

regular expression to replace Month-YY with YYYYMM

Posted on 2008-10-29
6
547 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

by Batuhan Cetin Regular expression is a language that we use to edit a string or retrieve sub-strings that meets specific rules from a text. A regular expression can be applied to a set of string variables. There are many RegEx engines for u…
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…

705 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