Im currently using regex101 to try and work out the following, id like to be able to capture a full items data for example name_template_2 and its associated description, define and write data
Here's my data model
templates:
name_template:
description: test_description
define: yes
write: true
name_template_2:
description: test_description2
define: false
write: true
I can capture the lines I need with the following
^[[:space:]][[:space:]][[:space:]][[:space:]].*
and
^[[:space:]][[:space:]]name_template_2:
but I am unable to join both patterns together to filter just the key and data related to name_template_2. The more I read online the more I understand it less. Has anyone achieved this before or is there a much more efficient way of doing this?
CodePudding user response:
I suggest using a structure-aware tool like yq to manipulate YAML and not using regular expressions.
#!/bin/bash
INPUT='
templates:
name_template:
description: test_description
define: yes
write: true
name_template_2:
description: test_description2
define: false
write: true
'
echo "$INPUT" | yq '{"name_template_2": .templates.name_template_2}'
Output
name_template_2:
description: test_description2
define: false
write: true
CodePudding user response:
Using 2 capture groups:
^[^\S\n]{2}(name_template_2:)((?:\n[^\S\n]{4}\S.*) )
Explanation
^
Start of string[^\S\n]{2}
Match 2 spaces without newlines(name_template_2:)
Match the string and capture in group 1(
Capture group 2(?:
Non capture group\n
Match a newline[^\S\n]{4}
Match 4 spaces without newlines\S.*
Match a non whitespace char and the rest of the line
)
Repeat the non capture group 1 or more times
)
Close group 2