Practical: Modifies the default alphanumeric token to include hyphens, allowing it to match hyphenated identifiers as single words.
ID: 1136
See: Tokens = [Tokens]
using uCalcSoftware;
var uc = new uCalc();
var t = new uCalc.Transformer();
t.FromTo("{@Alpha:word}", "[{word}]");
string text = "id-E123 is a special-identifier.";
Console.WriteLine("--- Before --- ");
// By default, 'id-123' is tokenized as three separate parts: 'id', '-', and '123'.
Console.WriteLine(t.Transform(text));
// Get the alphanumeric token item by its name and modify its regex.
var alphaToken = t.Tokens["_token_alphanumeric"];
alphaToken.Regex = "[a-zA-Z0-9-]+";
Console.WriteLine("");
Console.WriteLine("--- After --- ");
// Now, hyphenated words are matched as single alphanumeric tokens.
Console.WriteLine(t.Transform(text));
--- Before ---
[id]-[E123] [is] [a] [special]-[identifier].
--- After ---
[id-E123] [is] [a] [special-identifier]. using uCalcSoftware; var uc = new uCalc(); var t = new uCalc.Transformer(); t.FromTo("{@Alpha:word}", "[{word}]"); string text = "id-E123 is a special-identifier."; Console.WriteLine("--- Before --- "); // By default, 'id-123' is tokenized as three separate parts: 'id', '-', and '123'. Console.WriteLine(t.Transform(text)); // Get the alphanumeric token item by its name and modify its regex. var alphaToken = t.Tokens["_token_alphanumeric"]; alphaToken.Regex = "[a-zA-Z0-9-]+"; Console.WriteLine(""); Console.WriteLine("--- After --- "); // Now, hyphenated words are matched as single alphanumeric tokens. Console.WriteLine(t.Transform(text));
#include
#include "uCalc.h"
using namespace std;
using namespace uCalcSoftware;
int main() {
uCalc uc;
uCalc::Transformer t;
t.FromTo("{@Alpha:word}", "[{word}]");
string text = "id-E123 is a special-identifier.";
cout << "--- Before --- " << endl;
// By default, 'id-123' is tokenized as three separate parts: 'id', '-', and '123'.
cout << t.Transform(text) << endl;
// Get the alphanumeric token item by its name and modify its regex.
auto alphaToken = t.Tokens()["_token_alphanumeric"];
alphaToken.Regex("[a-zA-Z0-9-]+");
cout << "" << endl;
cout << "--- After --- " << endl;
// Now, hyphenated words are matched as single alphanumeric tokens.
cout << t.Transform(text) << endl;
}
--- Before ---
[id]-[E123] [is] [a] [special]-[identifier].
--- After ---
[id-E123] [is] [a] [special-identifier]. #include <iostream> #include "uCalc.h" using namespace std; using namespace uCalcSoftware; int main() { uCalc uc; uCalc::Transformer t; t.FromTo("{@Alpha:word}", "[{word}]"); string text = "id-E123 is a special-identifier."; cout << "--- Before --- " << endl; // By default, 'id-123' is tokenized as three separate parts: 'id', '-', and '123'. cout << t.Transform(text) << endl; // Get the alphanumeric token item by its name and modify its regex. auto alphaToken = t.Tokens()["_token_alphanumeric"]; alphaToken.Regex("[a-zA-Z0-9-]+"); cout << "" << endl; cout << "--- After --- " << endl; // Now, hyphenated words are matched as single alphanumeric tokens. cout << t.Transform(text) << endl; }
Imports System
Imports uCalcSoftware
Public Module Program
Public Sub Main()
Dim uc As New uCalc()
Dim t As New uCalc.Transformer()
t.FromTo("{@Alpha:word}", "[{word}]")
Dim text As String = "id-E123 is a special-identifier."
Console.WriteLine("--- Before --- ")
'// By default, 'id-123' is tokenized as three separate parts: 'id', '-', and '123'.
Console.WriteLine(t.Transform(text))
'// Get the alphanumeric token item by its name and modify its regex.
Dim alphaToken = t.Tokens("_token_alphanumeric")
alphaToken.Regex = "[a-zA-Z0-9-]+"
Console.WriteLine("")
Console.WriteLine("--- After --- ")
'// Now, hyphenated words are matched as single alphanumeric tokens.
Console.WriteLine(t.Transform(text))
End Sub
End Module
--- Before ---
[id]-[E123] [is] [a] [special]-[identifier].
--- After ---
[id-E123] [is] [a] [special-identifier]. Imports System Imports uCalcSoftware Public Module Program Public Sub Main() Dim uc As New uCalc() Dim t As New uCalc.Transformer() t.FromTo("{@Alpha:word}", "[{word}]") Dim text As String = "id-E123 is a special-identifier." Console.WriteLine("--- Before --- ") '// By default, 'id-123' is tokenized as three separate parts: 'id', '-', and '123'. Console.WriteLine(t.Transform(text)) '// Get the alphanumeric token item by its name and modify its regex. Dim alphaToken = t.Tokens("_token_alphanumeric") alphaToken.Regex = "[a-zA-Z0-9-]+" Console.WriteLine("") Console.WriteLine("--- After --- ") '// Now, hyphenated words are matched as single alphanumeric tokens. Console.WriteLine(t.Transform(text)) End Sub End Module