When writing TPL you will probably end up using regex.extract() most frequently, however don’t forget to check the TPL guide for its friend, regex.extractAll() which is useful when you need to extract several data items matching a single pattern.
This example shows its use, for extracting nameserver out of a Unix hosts’s DNS config file:
tpl 1.0 module TEST;
pattern TEST 1.0
'''
Demo to show how to use extractAll
'''
overview
tags TEST;
end overview;
triggers
on device := DeviceInfo where os_class = 'UNIX';
end triggers;
body
host := model.host(device);
if not host then
log.error('No host node for: % device.name%');
stop;
end if;
file := discovery.fileGet(host, '/etc/resolv.conf');
if file then
nameservers := regex.extractAll(file.content, regex '\nnameserver\s+(\S+)');
for nameserver in nameservers do
log.info('>>>> Detected nameserver % nameserver%');
end for;
end if;
end body;
end pattern;
