1# Copyright 2020 Northern.tech AS 2 3# This file is part of CFEngine 3 - written and maintained by Northern.tech AS. 4 5# This program is free software; you can redistribute it and/or modify it 6# under the terms of the GNU General Public License as published by the 7# Free Software Foundation; version 3. 8 9# This program is distributed in the hope that it will be useful, 10# but WITHOUT ANY WARRANTY; without even the implied warranty of 11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12# GNU General Public License for more details. 13 14# You should have received a copy of the GNU General Public License 15# along with this program; if not, write to the Free Software 16# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA 17 18# To the extent this program is licensed as part of the Enterprise 19# versions of Cfengine, the applicable Commercial Open Source License 20# (COSL) may apply to this file if you as a licensee so wish it. See 21# included file COSL.txt. 22 23#+begin_src cfengine3 24bundle agent main 25{ 26 vars: 27 # global regex replace A with B 28 "AB" string => regex_replace("This has AAA rating", "A", "B", "g"); 29 # global regex replace [Aa] with B (case insensitive) 30 "AaB" string => regex_replace("This has AAA rating", "A", "B", "gi"); 31 # global replace every three characters with [cap=thecharacters] using $1 32 "cap123" string => regex_replace("abcdefghijklmn", "(...)", "[cap=$1]", "g"); 33 # multiple captures using \1 \2 (just like $1 $2 but can only go up to \9) 34 "path_breakdown" string => regex_replace("/a/b/c/example.txt", "(.+)/(.+)", "dirname = \1 file basename = \2", ""); 35 36 reports: 37 # in order, the above... 38 "AB replacement = '$(AB)'"; 39 "AaB replacement = '$(AaB)'"; 40 "cap123 replacement = '$(cap123)'"; 41 "path_breakdown replacement = '$(path_breakdown)'"; 42} 43#+end_src 44############################################################################### 45#+begin_src example_output 46#@ ``` 47#@ R: AB replacement = 'This has BBB rating' 48#@ R: AaB replacement = 'This hBs BBB rBting' 49#@ R: cap123 replacement = '[cap=abc][cap=def][cap=ghi][cap=jkl]mn' 50#@ R: path_breakdown replacement = 'dirname = /a/b/c file basename = example.txt' 51#@ ``` 52#+end_src 53