Need help creating REGEX Search and Replace

Events happening in the community are now at Drupal community events on www.drupal.org.
JSCSJSCS's picture

I am importing CSV records using Feeds module and Feeds Tamper. I need help figuring out a proper REGEX search and replace expression.

The first character is a numerical digit to represent the year. Example: the "3" in "3M" is a code for the year 2013 (or 2023, 2033, etc.).

The second character is a letter representing the month as follows:

A JAN
B FEB
C MAR
D APR
E MAY
F JUN
G JUL
H AUG
I (NOT USED)
J SEP
K OCT
L NOV
M DEC

So the code "3M" is actually 01-DEC-2013 (The day is always the first of the month)

Could use some REGEX Guru to help out if possible. It would take me days to figure out, I'm sure.

Thanks!

Comments

Regex might not be enough

chellman's picture

So you need to transform things like this:

3M
3B
4K

Into:

01-DEC-2013
01-FEB-2013
01-OCT-2014

and so on?

You can use a regex to extract the parts:

/(\d)([A-H,J-M])/

That would give you the digit in \1 and the letter in \2, but the transformation into the dates would require a bit more processing then a regex can give you. I haven't used Feeds Tamper before, so I don't know if you're restricted to just regular expressions, but hopefully you could write a little function that would map the letters and digits to the properly formatted dates, and use that as a callback in your feed tampering.

Do you know of any general,

kurtronaldmueller@gmail.com's picture

Do you know of any general, helpful resources about regex?

Kurt

Tools for Regexes

mlazar's picture

Hi,

I've been a fan of "The Regex Coach" for some time. Simple interface and effective. Not sure what is out there for the mac. a mini php script perhaps?
-m

This site is a great Regex

jalake's picture

This site is a great Regex information site as well as having some high quality tutorials. I've learned quite a bit from it: http://www.regular-expressions.info/

-J

RegEx novice lesson

bvirtual's picture

http://peterbenjamin.com/seminars/regexp/regular.expressions.lesson2.1.html

I wrote this lesson based upon the principle that once you could "read" an RE, then you could write one.

Peter

LA's Open Source User Group Advocate - Volunteer at DrupalCamp LA and SCALE

Working Feeds/Feeds Tamper CVS field Import

JSCSJSCS's picture

Well I got it working, but it is not pretty.

First I had to create a new column for the calculated "Expiration Date" in the cvs file. The cvs parser does not have a "holding place" like the RSS feed parser does. So you can't use Feeds Tamper to tamper with a column that does not exist in your cvs file. This means you have to add it manually.

Then I used Feeds to map the empty "Expiration Date" column of the cvs file to the content type field that will hold the calculated "Expiration Date".

Then I used Feeds Tamper to add several plugins in a particular order.

First I used a Rewrite plugin to write the contents of the provided (but coded) expire date content type field "xprdte" data values (example: 3M) imported and mapped from the cvs file to the "Expiration Date" field.

Next I used a Find Replace REGEX plugin to swap the two characters and add the begining of the year"

FIND: /([0-9])([A-H,J-M])/
REPLACE: $2-201$1

So now "3M" was held as "M-2013"

Then I added 12 more regular Find Replace plugins for each month represented by the Capital letter:
FIND: M-
REPLACE: 01-DEC-

So now "M-2013" was held as "01-DEC-2013"

This will work for a few years I guess

One note, I had to rearrange the order of the date plugins so that the one for "L-" (NOV) was executed before the one for "J-" (JUL), otherwise, after correctly manipulating "3L" to 01-JUL-2013, the November manipulation plugin would run and change it to "01-JU01-NOV-2013"

I don't think REGEX can actually FIND & REPLACE several different characters with several other different ones, not that I could figure out anyway.

Thanks for all the suggestions. They really helped!

James Sinkiewicz
Drupal Site Builder and Generalist
http://MyDrupalJourney.com

Regexes are powerful, but also limited

chellman's picture

I don't think REGEX can actually FIND & REPLACE several different characters with several other different ones, not that I could figure out anyway.

That's exactly right. Mapping of particular items to other particular items is not doable with regular expressions alone. Their power is in finding the matches, not in making the substitutions.

Please, help with regex

dr.osd's picture

I need to delete all character after "-". For example: convert qwe123-15 to qwe123 or asd321-5 to asd321. How can I do it?

Truncate String

jeoware's picture

What I do is this:
$pos = strpos($var, '-');
if ($pos >0){
$var = substr($var, 0, $pos-1); // usually, I assign to a new variable.
// if you want, do a print($var); here.
}

Thanks..

dr.osd's picture

Thanks, but I want to use standart tamper with plugin "Find and repalce REGEX".

to find:

suegmune's picture

to find: /-.*$/
replace:
(leave replace blank)

Thanks, that helped me out!

maxplus's picture

Thanks,
that helped me out!

San Diego DUG

Group categories

Event Classifications

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds:

Hot content this week