You may often want to find all pattern matches within the device configuration and filter the results based on a regular expression.
The following NQE query uses a regex filter as part of a two step process in the getMatch(device)
function.
First, we find peer-group names in a Cisco BGP configuration and stores each match in a string called groupName by matching on the pattern
defined at the top of the query.
Second, we iterate through each each pattern match and filter the groupName based on the regex `^PUBLIC.*(?:-4|-X)$`
The regex matches a string that starts with the word “PUBLIC” and end with either “-4” or “-X”.
The main foreach loop at the bottom of the query applies the function getMatch
to output a list of devices along with all the matching peer group names
pattern = ```
router bgp {number}
neighbor {groupName: string} peer-group
```;
getMatch(device) =
foreach match in blockMatches(device.files.config, pattern)
let validRegex = re`^PUBLIC.*(?:-4|-X)$`
let groupName = match.data.groupName
where hasMatch(groupName, validRegex)
select { groupName: groupName};
foreach device in network.devices
let matches = getMatch(device)
where length(matches) > 0
select {
"Device Name": device.name,
"Peer Group": (foreach entry in matches select entry.groupName)
}