* Remove extra backtick * Reword explanation of update * Reword explanation of relative update * Change "remaple" to "remain" * Change "clovver" to "clobber" * Reword explanation of update for comment operators * Reword explanation of relative update for comment operators * Change "array" to "expression" * Change "the golangs" to "Golang's" * Change "golangs" to "Golang's" * Change "can durations" to "can add durations" * Change "array scalars" to "arrays" * Change "beit" to "be it" * Fix typo in `eval` tip * Fix typo in header for `has` operation * Add space before pipe in `line` operator example * Fix typos in explanation of deep array merges * Change "is now used" to "is now used." * Change "object," to "object." * Changes "indexes" to "indices" * Remove extraneous copied text from `..` article * Reword explanation of `...` operator * Change "your are" to "you are" * Add link to `string` operator docs in `select` article * Change "is a" to "parameter specifies" in `string` operators article * Change "new line" to "newline" * Change "golang regex" to "Golang's regex" * Change "golang" to "Golang" * Add period * Remove comma in `subtract` article * Remove duplicate number subtraction example * Remove comma in `traverse` operator article * Clarify use of brackets when `read`ing with special characters
1.6 KiB
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:
a: |
cat
Using $( exp )
wont work, as it will trim the trailing newline.
m=$(echo "cat\n") yq -n '.a = strenv(m)'
a: cat
However, using printf works:
printf -v m "cat\n" ; m="$m" yq -n '.a = strenv(m)'
a: |
cat
As well as having multiline expressions:
m="cat
" yq -n '.a = strenv(m)'
a: |
cat
Similarly, if you're trying to set the content from a file, and want a trailing newline:
IFS= read -rd '' output < <(cat my_file)
output=$output ./yq '.data.values = strenv(output)' first.yml