The template toolkit contains the first 20 chrachters per line

I am trying to remove everything from the string except the first 20 characters. But the regex removes the first 20 and saves the rest. How can i solve this?

Here is my code:

[% p.name | remove('(.{20})', '$1') %] 

I also tried this:

 [% p.name.replace('(.{20})', '$1')) %] 
+4
source share
1 answer

Your regular expression will not match a string whose length is less than 20.

Using:

 [% p.name.replace('(?<=.{20}).*', '')) %] 

Or:

 [% p.name.replace('(.{0,20}).*', '$1')) %] 
+2
source

Source: https://habr.com/ru/post/1439135/


All Articles