We use cookies to improve your experience. No personal information is gathered and we don't serve ads. Cookies Policy.

ExpressionEngine Logo ExpressionEngine
Features Pricing Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University
Log In or Sign Up
Log In Sign Up
ExpressionEngine Logo
Features Pro new Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University Blog
  • Home
  • Forums

regex in :replace variable modifier

How Do I?

JCOGS Design's avatar
JCOGS Design
71 posts
6 years ago
JCOGS Design's avatar JCOGS Design

Hi All. Any gurus at EE’s interpretation of regex (specifically as it applies to the :replace variable modifier)? I am trying to replace the last character of lines of this type with a semi-colon.

('293','10','292','7','1'), ('293','10','292','7','2'), ('293','11','292','8','1'), ('293','11','292','8','2'),

In VSC (and checked via regex101.com) this pattern matches all but the last character of the line : /(.*)./ and so the substitution pattern $1; gives the revised line I am after.

When I apply these patterns via the :replace modifier (i.e.

{value_to_update:replace find='/(.*)./' replace='$1;' regex='yes'}

EE replaces all the commas after ) with semi colons - so I end up with

('293','10','292','7','1'); ('293','10','292','7','2'); ('293','11','292','8','1'); ('293','11','292','8','2');

Does anyone have any hints as to how I can change the regex to get the outcome I want?

       
Derek Jones's avatar
Derek Jones
7,561 posts
6 years ago
Derek Jones's avatar Derek Jones

ExpressionEngine just passes your “find” pattern on to PHP’s preg_replace(), which on your source string works as expected. I suspect your input is not what you think it is, and that instead of a space after the commas you have a newline. Which causes the pattern to only match each line, thus replacing all the commas with semicolons. If that’s the case, you can either address the input if it’s stored improperly, or just use the s pattern modifier to ensure dots also match newline characters, e.g. /(.*)./s

See here.

       
JCOGS Design's avatar
JCOGS Design
71 posts
6 years ago
JCOGS Design's avatar JCOGS Design

Of course - was looking at rendered data not source. Doh! Thanks alot for the suggestion - will get on with it.

       

Reply

Sign In To Reply

ExpressionEngine Home Features Pro Contact Version Support
Learn Docs University Forums
Resources Support Add-Ons Partners Blog
Privacy Terms Trademark Use License

Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.