Solved

regular expression to replace Month-YY with YYYYMM

Posted on 2008-10-29
6
531 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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

914 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

19 Experts available now in Live!

Get 1:1 Help Now