Regex in Go: How to replace only the capture group? [duplicate] - regex

I've used regexp package to replace bellow text
{% macro products_list(products) %}
{% for product in products %}
productsList
{% endfor %}
{% endmacro %}
I could not replace "products" without replace another words like "products_list" and Golang has no a func like re.ReplaceAllStringSubmatch to do replace with submatch (there's just FindAllStringSubmatch). I've used re.ReplaceAllString to replace "products" with .
{% macro ._list(.) %}
{% for product in . %}
.List
{% endfor %}
{% endmacro %}
But I need this result:
{% macro products_list (.) %}
{% for product in . %}
productsList
{% endfor %}
{% endmacro %}

You can use capturing groups with alternations matching either string boundaries or a character not _ (still using a word boundary):
var re = regexp.MustCompile(`(^|[^_])\bproducts\b([^_]|$)`)
s := re.ReplaceAllString(sample, `$1.$2`)
Here is the Go demo and a regex demo.
Notes on the pattern:
(^|[^_]) - match string start (^) or a character other than _
\bproducts\b - a whole word "products"
([^_]|$) - either a non-_ or the end of string.
In the replacement pattern, we use backreferences to restore the characters captured with the parentheses (capturing groups).

Related

Django show list of strings in textarea, each string from new line

I have a list lst=['apple', 'pear', 'peach'].
I want to show each item in textarea from new line (removing redundant subspaces).
<textarea>
{% for item in lst %}
{{ item }}
{% if not forloop.last %}<br/>{% endif %}
{% endfor %}
</textarea>
This code doesn't work. <br/>s are shown as they are. And there are redundant whitespaces at the beginning of the textarea and between the items.
The <textarea> tag is very stupid:) in your template there is no place for new line and space:
<textarea>{% for item in lst %}{{ item }}{% if not forloop.last %}
{% endif %}{% endfor %}</textarea>
It need to be in one line or it will break.
Line Feed and 
 Carriage Return as #Bukudan explain in this thread: https://stackoverflow.com/a/8627926/13773284

Using expressions inside tags

I'd like to capitalize a string variable and translate it at the same time, but i can't find how to do this in the api docs.
i.e. this is throwing an error:
{% trans {{ someString | capfirst }} %}
In Django if you start curly brackets {{ or {%, then inside you cannot open another one. Just use it, as you would use it without extra brackets.
{% trans {{ someString | capfirst }} %}
# change to:
{% trans someString|capfirst %}
PS. As I think it's not necessary, it's a good practice not to put spaces right before and next to |.

RegEx - skip text between quotes

how can I skip some text which is between quotes("text") or brackets(<div>text<div>) or anything else
here is an example where I have a Django template and I want to find the tags, where I have intentionally injected tags inside a string.
{% load humanize %}
{% url "core:p%}os{%t" post.pk %} {% url "index" %}
and this is the regex currently I have
\{%\s*\b(url|load)\b.*?\s*%\}
What this regex matches is
{% url "core:p%}
But obviously that is not what I want. I want to match the whole tag like this
{% url "core:p%}os{%t" post.pk %}
Here is the regexer link, where you can test it.

Twig: negate containment operator IN

I'm trying to negate IN.
Basically, I want to output true, when keyword1 is in keywords, but keyword11 isn't.
Please note: keyword11 ("my_keyword_11") starts with keyword1 ("my_keyword_1"). keywords has to stay a string, can't split/explode by the comma.
{% set keywords = 'my_keyword_1, my_keyword_2' %}
{% set keyword1 = 'my_keyword_1' %}
{% set keyword11 = 'my_keyword_11' %}
{% if ((keyword1 in keywords) and (keyword11 not in keywords)) %}
true
{% endif %}
Any ideas? Thanks in advance!
Your code is ok as you can view in this twigfiddle

Is there a way to split text on whitespace in Liquid?

I'm trying to split a Jekyll post's contents into words, and have tried the following:
{% for word in post.content | split:' ' %}
{% do some stuff %}
{% endfor %}
Unfortunately this doesn't do anything; 'word' ends up as the whole post. I'm using this code on Github Pages, so unfortunately I can't write a plug-in to take care of this. Am I using the split filter incorrectly? Does Liquid support what I'm trying to do?
It seems that you can split on whitespace by using split: .
So you can try something like:
{% capture words %}{{ post.content | split: }}{% endcapture %}
or:
{% assign words = post.content | split: %}
From what I've tested so far it seems that you should use the latter (assign tag), as the capture tag seems to do an implicit join on the array elements when assigning the value to the variable.
Using:
{% for post in site.posts limit:1 offset:6 %}
{% assign words = post.content | split: %}
{% for word in words %}{{ word }} {% endfor %}
{% endfor %}
seems reproduce the post content in its entirety. The whitespace in the inner for loop matters.
Just as a note now, if you need to join some of the words back together with whitespace, the join tag seems to require quotes around the character, like so: join:' '.
Edit:
I ended up attempting to also do some splitting on whitespace, and while it worked in my development environment it didn't work on Github Pages. It looks like Pages is running version 2.2.2, whereas the split() filter was introduced in version 2.3.0. My development environment was running 2.4.1. Hopefully we can pester the fine folks at Github enough to get them to update their version of Liquid. :)
Filters (such as split) can only be used on {{ outputs }} not on {% tags %}.
You might be able to accomplish the split by using the capture function as follows:
{% capture 'foo' %} {{ post.content | split:' ' }} {% endcapture %}