Why would you want to match anything in a YAML file? Load it into Ruby using the YAML parser, and search it, or modify in memory.
If you want to save the modified file, the YAML parser can emit a Ruby object as YAML, which you then save.
require 'yaml'
yaml = '
---
Groups:
GroupA:
- item 1
- item 3
Group B:
- itemA
- item 3
C:
- 1
- item 3
'
yaml = YAML.load(yaml)
# => {"Groups"=>{"GroupA"=>["item 1", "item 3"], "Group B"=>["itemA", "item 3"], "C"=>[1, "item 3"]}}
yaml['Groups']['GroupA'].first
# => "item 1"
yaml['Groups']['Group B'][1]
# => "item 3"
yaml['Groups']['C'].last
# => "item 3"
Based on the above definitions, manipulating the data could be done like this:
yaml = YAML.load(yaml)
groups = yaml['Groups']
new_group = {
'groupa_first' => groups['GroupA'].first,
'groupb_second' => groups['Group B'][1],
'groupc_last' => groups['C'].last
}
yaml['New Group'] = new_group
puts yaml.to_yaml
Which outputs:
---
Groups:
GroupA:
- item 1
- item 3
Group B:
- itemA
- item 3
C:
- 1
- item 3
New Group:
groupa_first: item 1
groupb_second: item 3
groupc_last: item 3
There's a reason we have YAML parsers for all the different languages; They make it easy to load and use the data. Take advantage of that tool, and use Ruby to modify the data, and, if needed, write it out again. It would be one huge YAML file before I'd even think of trying to modify it on disk considering it's so easy to do in memory.
Now, the question becomes, how do you search the keys of a hash using a regex?
yaml['Groups'].select{ |k,v| k[/^Group/] }
# => {"GroupA"=>["item 1", "item 3"], "Group B"=>["itemA", "item 3"]}
Once you have the ones you want, you can easily modify their contents, substitute them back into the in-memory hash, and write it out.