|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
gregw Smarty Rookie
Joined: 03 Jan 2017 Posts: 6
|
Posted: Tue Jan 03, 2017 7:31 pm Post subject: Help with Regex_Replace |
|
|
I am am trying to trim an author field that looks like:
Flaker, G. C., Theriot, P., Binder, L. G., Dobesh, P. P., Cuker, A., Doherty, J. U.
(Often there are fewer than 3 authors on the list.)
Client wants that to include ONLY 3 authors, followed by 'et al.' So, I've been trying to use regex_replace to find everything AFTER the first 6 commas.
I have some logic that seems to make sense:
Code: | {{$items->dc->creator|regex_replace:'[^,]*,[^,]*,[^,]*,[^,]*,[^,]*,[^,]*,(.*)':' et al'}} |
https://regex101.com/r/b8lcwc/1
And this:
Code: | {{$items->dc->creator|regex_replace:'/^(?:[^,]*\,){6}([^,]*)/':' et al'}} |
https://regex101.com/r/b8lcwc/6
But neither works in the email platform I'm building them in, BlueHornet. (Hence, the double curlies {{}}).
Am I missing something here? Any ideas? |
|
Back to top |
|
bsmither Smarty Elite
Joined: 20 Dec 2011 Posts: 322 Location: West Coast
|
Posted: Thu Jan 05, 2017 9:31 pm Post subject: |
|
|
Does BlueHornet strip the outer-most braces off before Smarty gets the template? |
|
Back to top |
|
bsmither Smarty Elite
Joined: 20 Dec 2011 Posts: 322 Location: West Coast
|
Posted: Thu Jan 05, 2017 9:55 pm Post subject: |
|
|
So, BlueHornet is an email solution. You enclose data elements within braces.
By the time the template is submitted to BlueHornet, the data elements should already be there. I am familiar with such things (Gammadyne).
If Smarty gets to it first, try this:
Code: | {ldelim}{smarty_code}{rdelim}
|
This outputs:
|
|
Back to top |
|
gregw Smarty Rookie
Joined: 03 Jan 2017 Posts: 6
|
Posted: Thu Jan 05, 2017 10:18 pm Post subject: |
|
|
bsmither wrote: | Does BlueHornet strip the outer-most braces off before Smarty gets the template? |
For whatever reason, BH requires that smarty tags be in double {{}}. Aside from that (and a few features they've seemed to arbitrarily disabled), Smarty seems to work well (though I can not pin them down on an answer to this question, so suspect something else is up). |
|
Back to top |
|
gregw Smarty Rookie
Joined: 03 Jan 2017 Posts: 6
|
Posted: Thu Jan 05, 2017 10:20 pm Post subject: |
|
|
bsmither wrote: | So, BlueHornet is an email solution. You enclose data elements within braces.
By the time the template is submitted to BlueHornet, the data elements should already be there. I am familiar with such things (Gammadyne).
If Smarty gets to it first, try this:
Code: | {ldelim}{smarty_code}{rdelim}
|
This outputs:
|
The code that I am currently using at BH uses Smarty extensively, and everything else works as expected (even with double {{}}). I cannot for the life of me, however, get this specific regex_replace logic to work. |
|
Back to top |
|
bsmither Smarty Elite
Joined: 20 Dec 2011 Posts: 322 Location: West Coast
|
Posted: Thu Jan 05, 2017 10:57 pm Post subject: |
|
|
I see. You say the regex expression should work, but doesn't match.
Try using quotes (double-quotes) instead of apostrophes (single-quotes).
Also, the docs seem to show that the expression is not only of type string delimited by quotes, but the expression itself has matching delimiters. The example uses slashes, but the expression delimiter character can be any character not used elsewhere in the expression. So if the expression left delimiter is #, then # must only next appear at the end of the expression as its right delimiter.
In your examples above, the first does not have expression delimiters, but the second does.
If BlueHornet uses Smarty, then everything must work together with the double-paired braces as a matter of course. So, you have convinced me that doubled braces isn't the cause of the problem. |
|
Back to top |
|
bsmither Smarty Elite
Joined: 20 Dec 2011 Posts: 322 Location: West Coast
|
Posted: Thu Jan 05, 2017 11:20 pm Post subject: |
|
|
Also, I think the capturing strategy is not correct.
When (if) the expression matches, you don't want to toss the whole thing away. You want to keep displaying part of it, and replace the rest.
So, this value having more than six commas:
Code: | regex_replace:"/([^,]*,[^,]*,[^,]*,[^,]*,[^,]*,[^,]*,).*/":"\\1 et al" |
Last edited by bsmither on Thu Jan 05, 2017 11:58 pm; edited 1 time in total |
|
Back to top |
|
bsmither Smarty Elite
Joined: 20 Dec 2011 Posts: 322 Location: West Coast
|
Posted: Thu Jan 05, 2017 11:54 pm Post subject: |
|
|
And the second example has (?: which does not capture, but that's exactly what you want.
Code: | "/^([^,]*,){6}/"
with \\1 having max 3 names with two commas each
assuming since sixth comma there is extra to be dropped
|
Of course, no match, no replacement. |
|
Back to top |
|
gregw Smarty Rookie
Joined: 03 Jan 2017 Posts: 6
|
Posted: Fri Jan 06, 2017 1:52 am Post subject: |
|
|
THANK YOU, bsmither. I am a regex noob and this has plagued me for too long. This worked:
Code: | {{$authors|regex_replace:"/([^,]*,[^,]*,[^,]*,[^,]*,[^,]*,[^,]*,).*/":"\\1 et al"}} |
This returns the latter half (all AFTER the first 3 authors):
Again, THANK YOU![/code] |
|
Back to top |
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|
|