You are here:  » Regular Expression - 'explode filter'


Regular Expression - 'explode filter'

Submitted by ChrisNBC on Fri, 2015-06-05 11:43 in

Hi David,

Hope all is going well.

I wondered if you may be able to tell me if there are any existing filters which would allow me to 'explode' size information (using regular expression) from a merchants feed. I currently use a number of filters to separate size information from a variety of fields into a dedicated size field. This works well except recently I started using a couple of new merchant feeds where the size is included within the 'product name' field but it can be in a variety of formats (e.g 34R, 34x32, 40 x-Long) and positions within this field. The existing 'explode' filters which I already use are limited in their effectiveness due to the lack of character strings to explode on. I wondered if there might already exist any filters which would allow me to specify the regular expression format of the size and then place the result in another field only if matched?

Thanks in advance.

Regards
Chris

Submitted by support on Fri, 2015-06-05 15:40

Hello Chris,

Sure - have a go with the following;

  /*************************************************/
  /* regexpUpdateOtherField */
  /*************************************************/
  $filter_names["regexpUpdateOtherField"] = "RegExp Update Other Field";
  function filter_regexpUpdateOtherFieldConfigure($filter_data)
  {
    print "Match (Perl RegExp):<br />";
    print "<input type='text' size='40' name='match' value='".widget_safe($filter_data["match"])."' />";
    widget_errorGet("match");
    print "Field:<br />";
    print "<input type='text' size='40' name='field' value='".widget_safe($filter_data["field"])."' />";
    widget_errorGet("field");
  }
  function filter_regexpUpdateOtherFieldValidate($filter_data)
  {
    if (!$filter_data["match"])
    {
      widget_errorSet("match","required field");
    }
    if (!$filter_data["field"])
    {
      widget_errorSet("field","required field");
    }
  }
  function filter_regexpUpdateOtherFieldExec($filter_data,$text)
  {
    global $importRecord;
    if (preg_match($filter_data["match"],$text,$matches))
    {
      $importRecord[$filter_data["field"]] = $matches[0];
    }
  }

This will add a "RegExp Update Other Field" filter which should work as you've described. The Match value should be a bracketed regular expression, and the Field value the db field name (refer to $config_fieldSet). To match a single value e.g. 34R simply use as the Match value:

(34R)

...or to test multiple values, you can use the | (or) operator:

(32R|34R|36R)

Or if you fancy getting very RegExpy(!) if a size can be recognised as 2 digits followed by S, R or L then this should work:

([0-9]{2}(S|R|L))

The more general the expression is made the more chance there would be a false match of course so in cases where the list is manageable I would suggest using exact values rather than wildcards...

Hope this helps!

Cheers,
David.
--
PriceTapestry.com

Submitted by ChrisNBC on Fri, 2015-06-05 21:05

Hi David,

Thanks for the attached which sounds perfect...I have just installed the filter but there seems to be a problem as there is no field to input the "Field value" into? Would be grateful if you could suggest how I might be able to resolve this.

Thanks in advance.

Regards
Chris

Submitted by support on Sat, 2015-06-06 07:22

Sorry about that Chris, the system name of the new filter was incorrect in the $filter_names array. Corrected above.

Cheers,
David.
--
PriceTapestry.com

Submitted by ChrisNBC on Sat, 2015-06-06 21:11

Hi David,

No problem, thanks for the quick response. I've modified the filter and it now works perfectly.

Thanks again for your help getting this to work.

Best regards
Chris