Using a context switch to prevent transformations inside a designated `[RAW]` block.

ID: 1027

				
					using uCalcSoftware;

var uc = new uCalc();
var t = new uCalc.Transformer();
string text = "Replace config, but not the one inside [RAW]this config is raw[/RAW].";

// Create a token set for the raw block that only tokenizes single characters.
var rawTransformer = new uCalc.Transformer();
var rawTokens = rawTransformer.Tokens;
rawTokens.Clear();
rawTokens.Add("."); // Match any single character

// Switch to rawTokens when [RAW] is found, and switch back at [/RAW].
t.Tokens.ContextSwitch(rawTokens, """
\[RAW\]
""", """
\[/RAW\]
""");

t.FromTo("config", "SETTING");

Console.WriteLine(t.Transform(text));
				
			
Replace SETTING, but not the one inside [RAW]this config is raw[/RAW].
				
					#include <iostream>
#include "uCalc.h"

using namespace std;
using namespace uCalcSoftware;

int main() {
   uCalc uc;
   uCalc::Transformer t;
   string text = "Replace config, but not the one inside [RAW]this config is raw[/RAW].";

   // Create a token set for the raw block that only tokenizes single characters.
   uCalc::Transformer rawTransformer;
   auto rawTokens = rawTransformer.Tokens();
   rawTokens.Clear();
   rawTokens.Add("."); // Match any single character

   // Switch to rawTokens when [RAW] is found, and switch back at [/RAW].
   t.Tokens().ContextSwitch(rawTokens, R"(\[RAW\])", R"(\[/RAW\])");

   t.FromTo("config", "SETTING");

   cout << t.Transform(text) << endl;
}
				
			
Replace SETTING, but not the one inside [RAW]this config is raw[/RAW].
				
					Imports System
Imports uCalcSoftware
Public Module Program
   Public Sub Main()
      Dim uc As New uCalc()
      Dim t As New uCalc.Transformer()
      Dim text As String = "Replace config, but not the one inside [RAW]this config is raw[/RAW]."
      
      '// Create a token set for the raw block that only tokenizes single characters.
      Dim rawTransformer As New uCalc.Transformer()
      Dim rawTokens = rawTransformer.Tokens
      rawTokens.Clear()
      rawTokens.Add(".") '// Match any single character
      
      '// Switch to rawTokens when [RAW] is found, and switch back at [/RAW].
      t.Tokens.ContextSwitch(rawTokens, "\[RAW\]", "\[/RAW\]")
      
      t.FromTo("config", "SETTING")
      
      Console.WriteLine(t.Transform(text))
   End Sub
End Module
				
			
Replace SETTING, but not the one inside [RAW]this config is raw[/RAW].