String Operators

RegEx

This uses Golang's native regex functions under the hood - See their docs for the supported syntax.

Case insensitive tip: prefix the regex with (?i) - e.g. test("(?i)cats").

match(regEx)

This operator returns the substring match details of the given regEx.

capture(regEx)

Capture returns named RegEx capture groups in a map. Can be more convenient than match depending on what you are doing.

test(regEx)

Returns true if the string matches the RegEx, false otherwise.

sub(regEx, replacement)

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.

String blocks, bash and newlines

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:

Interpolation

Given a sample.yml file of:

then

will output

Interpolation - not a string

Given a sample.yml file of:

then

will output

To up (upper) case

Works with unicode characters

Given a sample.yml file of:

then

will output

To down (lower) case

Works with unicode characters

Given a sample.yml file of:

then

will output

Join strings

Given a sample.yml file of:

then

will output

Trim strings

Given a sample.yml file of:

then

will output

Match string

Given a sample.yml file of:

then

will output

Match string, case insensitive

Given a sample.yml file of:

then

will output

Match with global capture group

Given a sample.yml file of:

then

will output

Match with named capture groups

Given a sample.yml file of:

then

will output

Capture named groups into a map

Given a sample.yml file of:

then

will output

Match without global flag

Given a sample.yml file of:

then

will output

Match with global flag

Given a sample.yml file of:

then

will output

Test using regex

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

Substitute / Replace string

This uses Golang's regex, described here. Note the use of |= to run in context of the current string value.

Given a sample.yml file of:

then

will output

Substitute / Replace string with regex

This uses Golang's regex, described here. Note the use of |= to run in context of the current string value.

Given a sample.yml file of:

then

will output

Custom types: that are really strings

When custom tags are encountered, yq will try to decode the underlying type.

Given a sample.yml file of:

then

will output

Split strings

Given a sample.yml file of:

then

will output

Split strings one match

Given a sample.yml file of:

then

will output

To string

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

Last updated

Was this helpful?