I'm trying to replace all the dot characters in a helm template string with \., like so:
{{- regexReplaceAllLiteral "\." "https://endpoint.index.up" "\\\." -}}
I want this to output - https://endpoint\.index\.up
This is giving me a parse error. I've tried a bunch of different combinations but nothing is replacing the dot characters. Any help appreciated!
If you're just trying to replace a fixed string, replace will be easier to use. (This is one of the Sprig string functions so it will work with other tools that also embed the Sprig template extensions.)
{{- "https://endpoint.index.up" | replace "." "\\." -}}
It doesn't look like backslash is treated specially in the replacement string argument to regexReplaceAllLiteral so it should work so long as the replacement string argument is backslash dot; inside the double-quoted string syntax you need to double the backslash but you do not need to escape the period, so again "\\." and not "\\\.".
I wanted to know exactly what is the difference between '>-' and '|-' especially in kubernetes yaml manifests
Newlines in folded block scalars (>) are subject to line folding, newlines in literal block scalars (|) are not.
Line folding replaces a single newline between non-empty lines with a space, and in the case of empty lines, reduces the number of newline characters between the surrounding non-empty lines by one:
a: > # folds into "one two\nthree four\n\nfive\n"
one
two
three
four
five
Line folding does not occur between lines when at least one line is more indented, i.e. contains whitespace at the beginning that is not part of the block's general indentation:
a: > # folds into "one\n two\nthree four\n\n five\n"
one
two
three
four
five
Adding - after either | or > will strip the newline character from the last line:
a: >- # folded into "one two"
one
two
b: >- # folded into "one\ntwo"
one
two
In contrast, | emits every newline character as-is, the sole exception being the last one if you use -.
Ok I got one main difference between > and | from here: https://docs.ansible.com/ansible/latest/reference_appendices/YAMLSyntax.html
Values can span multiple lines using | or >. Spanning multiple lines
using a “Literal Block Scalar” | will include the newlines and any
trailing spaces. Using a “Folded Block Scalar” > will fold newlines to
spaces; it’s used to make what would otherwise be a very long line
easier to read and edit. In either case the indentation will be
ignored.
Examples are:
include_newlines: |
exactly as you see
will appear these three
lines of poetry
fold_newlines: >
this is really a
single line of text
despite appearances
In fact the ">" is in my understanding, the equivalent of the escape characters '\' at the end of a bash script for example
If one can tell me what is the "-" used for in kubernetes yaml manifests it will complete my understanding :)
I have some trailing characters at the end of a string peregrinevwap^_^_
print "JH 4 - app: $application \n";
app: peregrinevwap^_^_
Do you know why they are there and how I can remove them. I tried the chomp command but this hasn't worked.
Check out the tr//cd operator to get rid of unwanted characters.
It's documented in "perldoc perlop"
$application =~ tr/a-zA-Z//cd;
Will remove everything except letters from the string and
$application =~ tr/^_//d;
Will remove all "^" and "_" characters.
If you only want to remove certain characters when they at the end of the string, use the s// search/replace operator with regular expressions and the $ anchor to match the end of the string.
Here's an example:
s/[\^_]*$//;
Let's hope the underscores do not occur at the end of your strings, otherwise you can't automatically separate them from these unwanted characters.
Are you sure these characters are actually ^ and _ characters?
^_ could also indicate Ctrl-Underscore, ASCII character 0x1F (Unit Separator). (Not a character I've ever seen used, but you never know.)
If this is in fact the case, you can remove them with something like:
$application =~ s/\x1F//g;
Here is a piece of code
while($l=~/(\\\s*)$/) {
statements;
}
$l contains a line of text taken form file, in effect this code is for go through lines in file.
Questions:
I don't clearly understand what the condition in while is doing. I think it is trying to match group of \ followed by some number of white spaces at the end of line and loop should stop whenever a line ends with \ and may be some white spaces. I am not sure of it.
I came across statement $a ~= s/^(.*$)/$1/ . What I understand that ^ will force matching at the beginning of string, but in (.*$) would mean match all the characters at the end of string . Dose it mean that the statement is trying to find if any group of character at the end is same as group of character in the beginning of text ?
It is interesting to note that this statement:
while ( $l =~ /(\\\s*)$/ ) {
Is an infinite loop unless $l is altered inside the loop so that the regex no longer matches. As has already been mentioned by others, this is what it matches:
( ... ) a capture group, captures string to $1 (that's the number one, not lower case L)
\\ matches a literal backslash
\s* matches 0 or more whitespace characters.
$ matches end of line with optional newline.
Since you do not have the /g modifier, this regex will not iterate through matches, it will simply check if there is a match, resetting the regex each iteration, thereby causing an endless loop.
The statement
$a ~= s/^(.*$)/$1/
Looks rather pointless. It captures a string of characters up until end of string, then replaces it with itself. The captured text is stored in $1 and is simply replaced. The only marginally useful thing about this regex is that:
It matches up until newline \n, and nothing further, which may be of some use to a parser. A period . matches any character except newline, unless the /s modifier is present on the regex.
It captures the line in $1 for future use. However, a simple /^(.*$)/ would do the same.
1. the while
Usually while (regex) is used with the /g modifier, otherwise, if it matches, you get an infinite loop (unless you exit the loop, like using last).
statements would be executed continuously in an infinite loop.
In your case, adding the g
while($l=~/(\\\s*)$/g)
will have the while make only one loop, due to the $ - making a match unique (whatever matches up to the end of string is unique, as $ marks the end, and there is nothing after...).
2. $a ~= s/^(.*$)/$1/
This is a substitution. If the string ^.*$ matches (and it will, since ^.*$ matches (almost, see comment) anything) it is replaced with... $1 or what's inside the (), ie itself, since the match occurs from 1st char to the end of string
^ means beginning of string
(.*) means all chars
$ end of string
so that will replace $a with itself - probably not what you want.
it matches a literal backslash followed by 0 or more spaces followed by the end of the line.
it executes statements for all the lines in that text file that contain a \, followed by zero or more spaces ( \s* ), at the end of the line ($).
It matches lines that end with a backslash character, ignoring any trailing whitespace characters.
Ending a line with a backslash is used in some languages and data files to indicate that the line is being continued on the next line. So I suspect this is part of a parser that merges these continuation lines.
If you enter a regular expression at RegExr and hover your mouse over the pieces, it displays the meaning of each piece in a tooltip.
(\\\s*)$ this regex means --- a \ followed by zero or more number of white space characters which is followed by end of the line. Since you have your regex in (...), you can extract what you matched using $1, if you need.
http://rubular.com/r/dtHtEPh5DX
EDIT -- based on your update
$a ~= s/^(.$)/$1/ --- this is search and replace. So your regex matches a line which contains exactly one character (since you use . http://www.regular-expressions.info/dot.html), except a new-line character. Since you use (...), the character which matched the regex is extracted and stored in variable a
EDIT -- you changed your regex so here is the updated answer
$a ~= s/^(.*$)/$1/ -- same as above except now it matches zero or more characters (except new-line)
Easy question but i could find answer here: http://twig.sensiolabs.org/doc/filters/date.html
I have this:
user.birthday|date("Y\m\d")
but the output is to example: 19930224
and i want to separate it buy "/" to example like: 1993/02/24
Any idea? Thx!
You cannot use backslash \ because it is used for escaping characters. You can use / instead.
In the date() function, you need to escape \ with a second one as \\.
And as said in the twig documentation:
To escape words and characters in the date format use \\ in front of
each character
So you need to escape it as following in your twig template:
{{ user.birthday|date('Y\\\\m\\\\d') }}