You are here:  » RegEx for Feed Filters


RegEx for Feed Filters

Submitted by seceleanu on Wed, 2017-03-01 18:45 in

Hey David,

Would it be possible to implement a RegEx filter (global or individual) within the feed management area? I would be practically similar to the Product Mapping RegEx, however it can be applied to all other fields.

One use case is to have the abillity to parse more than one info from the product name, for instance, and filling different other custom fields with the result.

Thanks,
Mihai

Submitted by support on Thu, 2017-03-02 09:31

Hello Mihai,

There is a Scan and Set filter in this thread and something very similar could be done using a RegExp match instead, against any other field in the feed, with a flexible Set option based on $n placeholders, exactly the same as Product Mapping RegExp. Have a go with the following code, add to the end of includes/filter.php just before the closing PHP tag:

  /*************************************************/
  /* scanSetRegexp */
  /*************************************************/
  $filter_names["scanSetRegexp"] = "Scan and Set RegExp";
  function filter_scanSetRegexpConfigure($filter_data)
  {
    widget_textBox("Field","field",TRUE,$filter_data["field"],"",3);
    widget_textBox("RegExp","regexp",TRUE,$filter_data["regexp"],"",3);
    widget_textBox("Set","set",TRUE,$filter_data["set"],"",3);
  }
  function filter_scanSetRegexpValidate($filter_data)
  {
    if (!$filter_data["field"])
    {
      widget_errorSet("field","required field");
    }
    if (!$filter_data["regexp"])
    {
      widget_errorSet("regexp","required field");
    }
    if (!$filter_data["set"])
    {
      widget_errorSet("set","required field");
    }
  }
  function filter_scanSetRegexpExec($filter_data,$text)
  {
    global $filter_record;
    if (preg_match($filter_data["regexp"],$filter_record[$filter_data["field"]],$matches))
    {
      $text = $filter_data["set"];
      foreach($matches as $k => $match)
      {
        $text = str_replace("\$".$k,$match,$text);
      }
    }
    return $text;
  }

For the field name, used the parsed field names exactly as shown in the Sample Data below the form on Feed Registration Step 2. For the RegExp, use brackets to identify the sub-expression parts you wish to use in the replacement, for example, if the tested field contains:

Foo 123 Bar

And you wanted to extract 123 but only when between Foo and Bar, you would use a RegExp as follows:

/Foo (.*) Bar/

And a Set value of

$1

Hope this helps!

Cheers,
David.
--
PriceTapestry.com

Submitted by richard on Thu, 2019-02-28 15:07

Hi David,

I'm looking to extract "{code saved} England"

from the following description:

All bedrooms are ensuite with hair dryer, tea and coffee making facilities, free WiFi, direct dial telephones and flat screen freeview TV. Address: {code saved} England

I have used the following with the above filter:
RegExp: /Address: (.*) /
Set: $1

The database, however, is populated with "{code saved}" omitting " England"

Apologies for my lack of knowledge, but despite googling I'm not sure why the end of the string is omitted. The database field is TEXT, so that is not the problem.

Thank you in advance!

Best regards,

Richard

Submitted by support on Thu, 2019-02-28 15:22

Hi Richard,

It looks like it's just because the description ends without a SPACE, but the value you are using for RegExp has a space following (.*) before the closing delimiter, so it is matching the SPACE after the postcode. If you use RegExp:

/Address: (.*)/

...that should work as expected...

Cheers,
David.
--
PriceTapestry.com

Submitted by richard on Thu, 2019-02-28 15:26

Great, that has done the job!

Thank you!