Page 1 of 1
ocmod regex not working
Posted: Sat Dec 21, 2019 7:06 pm
by guntis.e
Ocmod itself works. But my regex search is reported as not found. Modification not created. What am I missing? Opencart 3.0.3.2.
Code: Select all
<file path="catalog/view/theme/*/template/common/cart.twig">
<operation error="log" info="">
<search regex="true"><![CDATA[~{% for option in product\.option %}(.*)$~]]></search>
<add position="replace"><![CDATA[{% for option in product.option %}{% if option.name != 'test' %}$1]]></add>
</operation>
</file>
Error log:
FILE: catalog/view/theme/default/template/common/cart.twig
NOT FOUND - OPERATIONS ABORTED!
And here is the code of line 11,12 of cart.twig:
Code: Select all
{% for option in product.option %} <br />
- <small>{{ option.name }} {{ option.value }}</small> {% endfor %}
Re: ocmod regex not working
Posted: Sat Dec 21, 2019 7:36 pm
by cyclops12
You need to remove the backslash in your search DATA
<search regex="true"><![CDATA[~{% for option in product\.option %}(.*)$~]]></search>
Re: ocmod regex not working
Posted: Sat Dec 21, 2019 8:26 pm
by guntis.e
backslash escapes the dot. I have tried also without it - still the same. It must be something else, as i tried also just text in regex, like ~for option in product.option~, it did not help
Attaching the ocmod file, that seems perfectly valid, but is not working.
What i need to achieve is insert some code between {% for option in product.option %} and <br />
Re: ocmod regex not working
Posted: Sat Dec 21, 2019 11:00 pm
by guntis.e
I found the cure!!!
$ as search for end of line seems not working. Instead
[\n\r] can be used.
so the working code is
Code: Select all
<file path="catalog/view/theme/default/template/common/cart.twig">
<operation error="log" info="">
<search regex="true"><![CDATA[~({% for option in product\.option %})(.*[\r\n])~]]></search>
<add position="replace"><![CDATA[\1{# inline edit #}\2]]></add>
</operation>
</file>
And hey - regex is the way how to get around the missing ibefore and iafter in ocmod

Re: ocmod regex not working
Posted: Sat Dec 21, 2019 11:17 pm
by straightlight
And hey - regex is the way how to get around the missing ibefore and iafter in ocmod
Glad to see it's working. However, since you're only editing one TWIG file, regex in this case is not required since the indexes do not need to be used with constant line tracking since \n\r is being tracked identically for each of these lines. The only problem with the suggestion above is whenever a \n\r is not involved in the line tracking and might still be considered an operation skipped.
Re: ocmod regex not working
Posted: Sun Dec 22, 2019 10:40 pm
by JNeuhoff
Instead [\n\r] can be used.
Isn't that dangerous? This assumes Microsoft style line ending, as used on Windows, whereas OpenCart is usually hosted on Linux servers which uses \n only for line endings.
Re: ocmod regex not working
Posted: Mon Dec 23, 2019 2:10 am
by guntis.e
Isn't that dangerous? This assumes Microsoft style line ending, as used on Windows, whereas OpenCart is usually hosted on Linux servers which uses \n only for line endings.
[\n\r] assumes \n or \r
If I really will need to find line ending, I will use (\r\n|\n|\r), which should include any possible line ending - windows or Linux. Because, even if the server is Linux, files from windows workstations could be created there with \r\n line endings.
The lesson learned for me by solving this ocmod problem is a) the regex line endings $ are not working, b) I can edit inline with regex any line, even multiline.

Re: ocmod regex not working
Posted: Mon Dec 23, 2019 2:12 am
by straightlight
The lesson learned for me by solving this ocmod problem is a) the regex line endings $ are not working, b) I can edit inline with regex any line, even multiline.
However, the lines without \r\n needed to be identically found without those criteria will be skipped, as explained above by using this method.