Using {@Whitespace} and {@Exec} to Count Indentation (for Python or YAML-like text)
ID: 223
See: {@Whitespace}, Whitespace
using uCalcSoftware;
var uc = new uCalc();
// Using {@Whitespace} to Count Indentation
// A common use case for parsing structured text (like Python or YAML)
// is capturing the exact whitespace at the start of a line.
uc.DefineVariable("IndentLen");
var t = uc.NewTransformer();
t.Text = " Item 1"; // Indented by 4 spaces
// Capture the leading whitespace into 'w' and evaluate its length
t.FromTo("{@Whitespace:w} Item {id}", "<{@Self}>{@Exec: IndentLen = Length(w)}");
t.Transform();
// We can now analyze the captured whitespace
Console.WriteLine($"Indentation length: {uc.EvalStr("IndentLen")}");
Indentation length: 4 using uCalcSoftware; var uc = new uCalc(); // Using {@Whitespace} to Count Indentation // A common use case for parsing structured text (like Python or YAML) // is capturing the exact whitespace at the start of a line. uc.DefineVariable("IndentLen"); var t = uc.NewTransformer(); t.Text = " Item 1"; // Indented by 4 spaces // Capture the leading whitespace into 'w' and evaluate its length t.FromTo("{@Whitespace:w} Item {id}", "<{@Self}>{@Exec: IndentLen = Length(w)}"); t.Transform(); // We can now analyze the captured whitespace Console.WriteLine($"Indentation length: {uc.EvalStr("IndentLen")}");
#include
#include "uCalc.h"
using namespace std;
using namespace uCalcSoftware;
int main() {
uCalc uc;
// Using {@Whitespace} to Count Indentation
// A common use case for parsing structured text (like Python or YAML)
// is capturing the exact whitespace at the start of a line.
uc.DefineVariable("IndentLen");
auto t = uc.NewTransformer();
t.Text(" Item 1"); // Indented by 4 spaces
// Capture the leading whitespace into 'w' and evaluate its length
t.FromTo("{@Whitespace:w} Item {id}", "<{@Self}>{@Exec: IndentLen = Length(w)}");
t.Transform();
// We can now analyze the captured whitespace
cout << "Indentation length: " << uc.EvalStr("IndentLen") << endl;
}
Indentation length: 4 #include <iostream> #include "uCalc.h" using namespace std; using namespace uCalcSoftware; int main() { uCalc uc; // Using {@Whitespace} to Count Indentation // A common use case for parsing structured text (like Python or YAML) // is capturing the exact whitespace at the start of a line. uc.DefineVariable("IndentLen"); auto t = uc.NewTransformer(); t.Text(" Item 1"); // Indented by 4 spaces // Capture the leading whitespace into 'w' and evaluate its length t.FromTo("{@Whitespace:w} Item {id}", "<{@Self}>{@Exec: IndentLen = Length(w)}"); t.Transform(); // We can now analyze the captured whitespace cout << "Indentation length: " << uc.EvalStr("IndentLen") << endl; }
Imports System
Imports uCalcSoftware
Public Module Program
Public Sub Main()
Dim uc As New uCalc()
'// Using {@Whitespace} to Count Indentation
'// A common use case for parsing structured text (like Python or YAML)
'// is capturing the exact whitespace at the start of a line.
uc.DefineVariable("IndentLen")
Dim t = uc.NewTransformer()
t.Text = " Item 1" '// Indented by 4 spaces
'// Capture the leading whitespace into 'w' and evaluate its length
t.FromTo("{@Whitespace:w} Item {id}", "<{@Self}>{@Exec: IndentLen = Length(w)}")
t.Transform()
'// We can now analyze the captured whitespace
Console.WriteLine($"Indentation length: {uc.EvalStr("IndentLen")}")
End Sub
End Module
Indentation length: 4 Imports System Imports uCalcSoftware Public Module Program Public Sub Main() Dim uc As New uCalc() '// Using {@Whitespace} to Count Indentation '// A common use case for parsing structured text (like Python or YAML) '// is capturing the exact whitespace at the start of a line. uc.DefineVariable("IndentLen") Dim t = uc.NewTransformer() t.Text = " Item 1" '// Indented by 4 spaces '// Capture the leading whitespace into 'w' and evaluate its length t.FromTo("{@Whitespace:w} Item {id}", "<{@Self}>{@Exec: IndentLen = Length(w)}") t.Transform() '// We can now analyze the captured whitespace Console.WriteLine($"Indentation length: {uc.EvalStr("IndentLen")}") End Sub End Module