String Operators
Last updated
Was this helpful?
Last updated
Was this helpful?
This uses Golang's native regex functions under the hood - See their for the supported syntax.
Case insensitive tip: prefix the regex with (?i)
- e.g. test("(?i)cats")
.
This operator returns the substring match details of the given regEx.
Capture returns named RegEx capture groups in a map. Can be more convenient than match
depending on what you are doing.
Returns true if the string matches the RegEx, false otherwise.
Substitutes matched substrings. The first parameter is the regEx to match substrings within the original string. The second parameter specifies what to replace those matches with. This can refer to capture groups from the first RegEx.
Bash is notorious for chomping on precious trailing newline characters, making it tricky to set strings with newlines properly. In particular, the $( exp )
will trim trailing newlines.
For instance to get this yaml:
Using $( exp )
wont work, as it will trim the trailing newline.
However, using printf works:
As well as having multiline expressions:
Similarly, if you're trying to set the content from a file, and want a trailing newline:
Given a sample.yml file of:
then
will output
Given a sample.yml file of:
then
will output
Works with unicode characters
Given a sample.yml file of:
then
will output
Works with unicode characters
Given a sample.yml file of:
then
will output
Given a sample.yml file of:
then
will output
Given a sample.yml file of:
then
will output
Given a sample.yml file of:
then
will output
Given a sample.yml file of:
then
will output
Given a sample.yml file of:
then
will output
Given a sample.yml file of:
then
will output
Given a sample.yml file of:
then
will output
Given a sample.yml file of:
then
will output
Given a sample.yml file of:
then
will output
Like jq's equivalent, this works like match but only returns true/false instead of full match details
Given a sample.yml file of:
then
will output
Given a sample.yml file of:
then
will output
Given a sample.yml file of:
then
will output
When custom tags are encountered, yq will try to decode the underlying type.
Given a sample.yml file of:
then
will output
Given a sample.yml file of:
then
will output
Given a sample.yml file of:
then
will output
Note that you may want to force yq
to leave scalar values wrapped by passing in --unwrapScalar=false
or -r=f
Given a sample.yml file of:
then
will output
This uses Golang's regex, described .
Note the use of |=
to run in context of the current string value.
This uses Golang's regex, described .
Note the use of |=
to run in context of the current string value.