2

I am building an extension for Opencart 2.3.x and I need to replace all occurences of DIR_IMAGE with _DIR_IMAGE , so I came up with this regular expression that works with PHP alone, but not within a VQMod mod:

<operation error="log">
    <search position="replace"><![CDATA[/(.*?)(DIR_IMAGE)(.*?)/g]]></search>
    <add><![CDATA[$1_DIR_IMAGE$3]]></add>
</operation>

This PHP-only does work:

preg_replace('/(.*?)(DIR_IMAGE)(.*?)/g', '$1_DIR_IMAGE$3', $string);

Can anyone point me in the right direction? At this point I assume alternatives to the expression above are appreciated.

random_user_name
  • 25,694
  • 7
  • 76
  • 115
yoda
  • 10,834
  • 19
  • 64
  • 92
  • 2
    PHP regex does not support the `g` modifier. `preg_replace` by default replaces all non-overlapping occurrences. Besdies, you need to use `regex="true"` to be able to use a regex. `` – Wiktor Stribiżew Sep 11 '17 at 13:00
  • @AD7six because I am using VQMod, which is a thirdparty piece of code to override original files. It is the only way. – yoda Sep 11 '17 at 13:02
  • @WiktorStribiżew I tried with ~exp~ to no avail, and also /exp/ – yoda Sep 11 '17 at 13:03
  • 2
    Add the regex attribute and retry - `` – Wiktor Stribiżew Sep 11 '17 at 13:03
  • @WiktorStribiżew you were right, I forgot the regex attr, I rarely use it so I didnt even remembered .. Please write down your answer so I can mark it. Thank you! – yoda Sep 11 '17 at 13:05

1 Answers1

3

First of all, you need to tell the engine to use a regex with regex=true attribute.

Another issue is that g is not supported by PHP preg_replace, it replaces all occurrences by default.

Besides, you do not need the groups because .*? do not restrict the context (a hint: the .*? at the end of the pattern never matches anything, its group value is always an empty string because it is lazy and is not even tried), you may just use

<search regex="true" position="replace"><![CDATA[/DIR_IMAGE/]]></search>
<add><![CDATA[_DIR_IMAGE]]></add>

Note that a regex will make more sense if you need to restrict the context where you match DIR_IMAGE. If you need to match DIR_IMAGE that is not already preprended with _ use

<search regex="true" position="replace"><![CDATA[/(?<!_)DIR_IMAGE/]]></search>
                                                  ^^^^^^

Since you are using a CDATA block, there is no need to entitize < in the negative lookbehind (?<!_) that fails the match if there is _ immediately to the left of the current location.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • Don't know how to this applies to the op but the regex `/\bDIR_IMAGE\b/` (i.e. using word boundaries) is more appropriate than using capturing groups. – AD7six Sep 11 '17 at 13:09
  • @AD7six That is possible, but we do not have the actual requirements. Sure, word boundaries may restrict the matches and will make using a regex more sensible. I added a regex that will only match those `DIR_IMAGE`s that have no `_` immediately preceding the `DIR_IMAGE` substring. – Wiktor Stribiżew Sep 11 '17 at 13:10