You are here:  » Search and Replace RegExp_v1- Multiple Searching


Search and Replace RegExp_v1- Multiple Searching

Submitted by ChrisNBC on Sat, 2016-12-03 14:47 in

Hi David,

Hope all is going well.

I wondered if you might be able to give me some advice re a feed filter and it’s use…I’m using “Search and Replace RegExp_v1” to scan a size field so I can capture and replace part of the field with a word.

The regex I’m using is: /((.*)(y|Y|YR|yrs|Years)(.*))/i
Replace value is: $2 years fix

The field (after import) contains a range of sizes in the format

10 Years/Long,10 Years/Short,11 Years/Long,11 Years/Short,12 Years/Short,14 Years/Long,15 Years/Long,16 years FIX2

The raw field data looks like:

10 Years/Long,10 Years/Short,11 Years/Long,11 Years/Short,12 Years/Short,14 Years/Long,15 Years/Long,16 Years/Short

I have noticed that the filter only seems to replace the last matched word in the feed data field (even when the match should match and replace the word multiple times). I use the same filter a lot for other purposes without issue. I’m not sure if I have the regex wrong (I don’t think I have) or if the filter won’t actually capture and replace a number of times within the same field or if it’s the commas used as separators or even the '/' in the field which are causing the issue.

I wondered if you might be able to suggest the reason the above isn’t working?

Thanks in advance.

Best regards
Chris

Submitted by support on Mon, 2016-12-05 11:42

Hi Chris,

The reason for only making the last replacement is that preg_replace is "greedy" by default, in other words, it tries to match as much as possible for (.*). The "U" flag (ungreedy) can be used in some circumstances, but here I think it would be better to use \b (word boundary) instead of a wildcard match - have a go with;

/\b(y|Y|YR|yrs|Years)\b/i

Hope this helps!

Cheers,
David.
--
PriceTapestry.com

Submitted by ChrisNBC on Tue, 2016-12-06 14:18

Thanks David, I've never used word boundary before but that's sorted the issue perfectly.

Best regards
Chris