uCalc API Version: 5.7.0-preview.1 Released: 7/30/2026

Warning

uCalc API Preview Release Notice:This preview documentation describes the intended behavior of the API. It is not fully accurate or complete.The current preview build contains incomplete features, unoptimized performance, and is subject to breaking changes.Use of the preview version in your production code is not recommended.

Regex = [string]

Property

Product: 

Class: 

Gets or sets the regular expression pattern that defines how a token is recognized by the parser.

Remarks

⚙️ The Regex Property: Getter and Setter

The Regex property gets or sets the regular expression pattern associated with a token Item. This provides a powerful, programmatic way to inspect and modify the lexical rules of the parser at runtime.

Note: A token is not a separate class in uCalc. It is simply an Item with specific token-related properties.


📖 Getter: Inspecting Token Definitions

When used as a getter, this property provides introspection into uCalc's lexical analysis engine.

var pattern = myTokenItem.Regex;

This is invaluable for:

  • Debugging: Verifying that a custom token was defined with the correct regex pattern.
  • Dynamic Language Modification: Reading an existing token's regex, modifying it, and then setting it back.
  • Documentation Generation: Automatically creating a syntax reference by iterating through all defined tokens and printing their names and regex patterns.

✏️ Setter: Dynamic Syntax Modification

When used as a setter, this property allows you to alter the lexical rules of the parser dynamically.

myTokenItem.Regex = "[a-zA-Z]+";

Typically, a token's regex is assigned when it is first created with a method like Tokens.Add. The setter allows you to change that definition at runtime for any existing token, including the built-in ones that define the core expression language syntax. This enables you to:

  • Extend Syntax: Add support for new number formats (e.g., C-style hex 0x[0-9a-fA-F]+).
  • Change Language Rules: Modify the set of allowed characters in identifiers.
  • Adapt to Dialects: Adjust the parser to handle different variations of a language or data format.

💡 Why uCalc? Dynamic vs. Static Tokenizers

Most traditional parsing tools, like ANTLR or Lex/Flex, use a static approach:

  1. Define token rules in a separate grammar file (.g4, .l).
  2. Run a code generator to produce source code for a lexer.
  3. Compile this generated code into your application.

To change a token definition, you must modify the grammar file and recompile. This process is static and happens at compile-time.

uCalc's approach is fundamentally different and more flexible:

  • Runtime Modification: The token table is a live, mutable collection. You can add, remove, or modify token definitions at runtime using methods like ExpressionTokens().Add() and Regex = "...". There is no code generation or recompilation step.
  • Programmatic Control: Because the tokenizer is configured through an API, your application can adapt its own syntax on the fly. This enables powerful features like user-defined operators, dialect switching for different versions of a language, or creating sandboxed environments with restricted syntax.

In essence, the Regex property provides the tools to treat your application's language syntax as a dynamic, configurable entity rather than a static, compiled artifact.


⚠️ Best Practices & Performance

  • Scope of Impact: Be aware that changing a core token (like the one for numbers or identifiers via ExpressionTokens) will affect all subsequent parsing operations within that uCalc instance.
  • Initialization: It is generally best to configure custom token definitions once during your application's setup phase rather than repeatedly in a loop.

Examples

Displays the complete list of default token definitions, showing their type, internal name, and regex pattern.

ID: 70

				
					using uCalcSoftware;

var uc = new uCalc();
// Lists all tokens currently defined for the expression evaluator.

Console.WriteLine($"Token Count: {uc.ExpressionTokens.Count}");
Console.WriteLine("");
Console.WriteLine("Index  Type  Name: regex");
Console.WriteLine("========================");
var Tokens = uc.ExpressionTokens;
foreach(var token in Tokens) {
   Console.Write(Tokens.IndexOf(token));
   Console.WriteLine($"  {token.Description}  {token.Name}: {token.Regex}");
}

// Note that the expression evaluator token list has a few extra tokens,
// related to hex/bin/oct notation, string interpolation, and imaginary number
// notation, which are not found in the default Transformer token list.
				
			
Token Count: 30

Index  Type  Name: regex
========================
0  generic  _token_line: .*
1  generic  _token_catchall: .
2  generic  _token_catchall_utf8_other: [\xf0-\xf7][\x80-\xbf][\x80-\xbf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf][\x80-\xbf]|[\xc0-\xdf][\x80-\xbf]
3  generic  _token_punctuation: (--|\.{3}|\xE2\x80\xA6|[!"#$%&'()*+,\-./:;<=>?@\[\\\]^_`{|}~]|\xE2\x80[\x90-\x95])
4  generic  _token_quotechar: ("){3}|"|'
5  generic  _token_quotechar_single: '
6  generic  _token_quotechar_double: "
7  generic  _token_quotechar_tripledouble: """
8  memberaccess  _token_memberaccess: \.
9  generic  _token_variableargs: \.\.\.
10  reducible  _token_reducible2: [-:|+/*^&=%@!`\\<>?#$~]+
11  bracket  _token_parenthesis: \(
12  bracketclose  _token_parenthesis_close: \)
13  bracket  _token_curlybrace: \{
14  bracketclose  _token_curlybrace_close: \}
15  bracket  _token_squarebracket: \[
16  bracketclose  _token_squarebracket_close: \]
17  argseparator  _token_argseparator: ,
18  statementseparator  _token_newline: (?:\r?\n)|\r
19  statementseparator  _token_semicolon: ;
20  literal  _token_string_singlequoted: '([^']*(?:''[^']*)*)'
21  literal  _token_string_doublequoted: "([^"]*(?:""[^"]*)*)"
22  literal  _token_string_tripledoublequoted: """([\s\S]*?)"""
23  whitespace  _token_whitespace: [\t\v ]+
24  reducible  _token_reducible: [-:|+/*^&=%@!`\\<>?]+
25  literal  _token_floatnumber: [0-9]*\.?[0-9]+([eE][+-]?[0-9]+)?
26  alphanumeric  _token_alphanumeric: [a-zA-Z_][a-zA-Z0-9_]*
27  literal  _token_imaginaryunit: #i
28  tokentransform  _token_binaryhexoctalnotation: #[bho][0-9A-F]+
29  tokentransform  _token_stringinterpolationquote: \$['"]
				
					#include <iostream>
#include "uCalc.h"

using namespace std;
using namespace uCalcSoftware;

int main() {
   uCalc uc;
   // Lists all tokens currently defined for the expression evaluator.

   cout << "Token Count: " << uc.ExpressionTokens().Count() << endl;
   cout << "" << endl;
   cout << "Index  Type  Name: regex" << endl;
   cout << "========================" << endl;
   auto Tokens = uc.ExpressionTokens();
   for(auto token : Tokens) {
      cout << Tokens.IndexOf(token);
      cout << "  " << token.Description() << "  " << token.Name() << ": " << token.Regex() << endl;
   }

   // Note that the expression evaluator token list has a few extra tokens,
   // related to hex/bin/oct notation, string interpolation, and imaginary number
   // notation, which are not found in the default Transformer token list.
}
				
			
Token Count: 30

Index  Type  Name: regex
========================
0  generic  _token_line: .*
1  generic  _token_catchall: .
2  generic  _token_catchall_utf8_other: [\xf0-\xf7][\x80-\xbf][\x80-\xbf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf][\x80-\xbf]|[\xc0-\xdf][\x80-\xbf]
3  generic  _token_punctuation: (--|\.{3}|\xE2\x80\xA6|[!"#$%&'()*+,\-./:;<=>?@\[\\\]^_`{|}~]|\xE2\x80[\x90-\x95])
4  generic  _token_quotechar: ("){3}|"|'
5  generic  _token_quotechar_single: '
6  generic  _token_quotechar_double: "
7  generic  _token_quotechar_tripledouble: """
8  memberaccess  _token_memberaccess: \.
9  generic  _token_variableargs: \.\.\.
10  reducible  _token_reducible2: [-:|+/*^&=%@!`\\<>?#$~]+
11  bracket  _token_parenthesis: \(
12  bracketclose  _token_parenthesis_close: \)
13  bracket  _token_curlybrace: \{
14  bracketclose  _token_curlybrace_close: \}
15  bracket  _token_squarebracket: \[
16  bracketclose  _token_squarebracket_close: \]
17  argseparator  _token_argseparator: ,
18  statementseparator  _token_newline: (?:\r?\n)|\r
19  statementseparator  _token_semicolon: ;
20  literal  _token_string_singlequoted: '([^']*(?:''[^']*)*)'
21  literal  _token_string_doublequoted: "([^"]*(?:""[^"]*)*)"
22  literal  _token_string_tripledoublequoted: """([\s\S]*?)"""
23  whitespace  _token_whitespace: [\t\v ]+
24  reducible  _token_reducible: [-:|+/*^&=%@!`\\<>?]+
25  literal  _token_floatnumber: [0-9]*\.?[0-9]+([eE][+-]?[0-9]+)?
26  alphanumeric  _token_alphanumeric: [a-zA-Z_][a-zA-Z0-9_]*
27  literal  _token_imaginaryunit: #i
28  tokentransform  _token_binaryhexoctalnotation: #[bho][0-9A-F]+
29  tokentransform  _token_stringinterpolationquote: \$['"]
				
					Imports System
Imports uCalcSoftware
Public Module Program
   Public Sub Main()
      Dim uc As New uCalc()
      '// Lists all tokens currently defined for the expression evaluator.
      
      Console.WriteLine($"Token Count: {uc.ExpressionTokens.Count}")
      Console.WriteLine("")
      Console.WriteLine("Index  Type  Name: regex")
      Console.WriteLine("========================")
      Dim Tokens = uc.ExpressionTokens
      For Each token In Tokens
         Console.Write(Tokens.IndexOf(token))
         Console.WriteLine($"  {token.Description}  {token.Name}: {token.Regex}")
      Next
      
      '// Note that the expression evaluator token list has a few extra tokens,
      '// related to hex/bin/oct notation, string interpolation, and imaginary number
      '// notation, which are not found in the default Transformer token list.
   End Sub
End Module
				
			
Token Count: 30

Index  Type  Name: regex
========================
0  generic  _token_line: .*
1  generic  _token_catchall: .
2  generic  _token_catchall_utf8_other: [\xf0-\xf7][\x80-\xbf][\x80-\xbf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf][\x80-\xbf]|[\xc0-\xdf][\x80-\xbf]
3  generic  _token_punctuation: (--|\.{3}|\xE2\x80\xA6|[!"#$%&'()*+,\-./:;<=>?@\[\\\]^_`{|}~]|\xE2\x80[\x90-\x95])
4  generic  _token_quotechar: ("){3}|"|'
5  generic  _token_quotechar_single: '
6  generic  _token_quotechar_double: "
7  generic  _token_quotechar_tripledouble: """
8  memberaccess  _token_memberaccess: \.
9  generic  _token_variableargs: \.\.\.
10  reducible  _token_reducible2: [-:|+/*^&=%@!`\\<>?#$~]+
11  bracket  _token_parenthesis: \(
12  bracketclose  _token_parenthesis_close: \)
13  bracket  _token_curlybrace: \{
14  bracketclose  _token_curlybrace_close: \}
15  bracket  _token_squarebracket: \[
16  bracketclose  _token_squarebracket_close: \]
17  argseparator  _token_argseparator: ,
18  statementseparator  _token_newline: (?:\r?\n)|\r
19  statementseparator  _token_semicolon: ;
20  literal  _token_string_singlequoted: '([^']*(?:''[^']*)*)'
21  literal  _token_string_doublequoted: "([^"]*(?:""[^"]*)*)"
22  literal  _token_string_tripledoublequoted: """([\s\S]*?)"""
23  whitespace  _token_whitespace: [\t\v ]+
24  reducible  _token_reducible: [-:|+/*^&=%@!`\\<>?]+
25  literal  _token_floatnumber: [0-9]*\.?[0-9]+([eE][+-]?[0-9]+)?
26  alphanumeric  _token_alphanumeric: [a-zA-Z_][a-zA-Z0-9_]*
27  literal  _token_imaginaryunit: #i
28  tokentransform  _token_binaryhexoctalnotation: #[bho][0-9A-F]+
29  tokentransform  _token_stringinterpolationquote: \$['"]
Change characters accepted as alphanumeric in expressions using ExpressionTokens() & Token()

ID: 71

				
					using uCalcSoftware;

var uc = new uCalc();
// (See alternate version of this example using ItemOf instead of ExpressionTokens)

// In this section underscore, _, and numeric digits
// are accepted as part of alphanumeric tokens
uc.DefineVariable("My_Variable = 111");
Console.WriteLine(uc.Error.Message);
uc.DefineVariable("Variable123 = 222");
Console.WriteLine(uc.Error.Message);

Console.WriteLine(uc.ExpressionTokens[TokenType.AlphaNumeric].Regex);
Console.WriteLine(uc.EvalStr("My_Variable"));
Console.WriteLine(uc.EvalStr("Variable123"));
Console.WriteLine("---");

// Now we no longer want underscore, _, or numeric digits
// to be accepted in alphanumeric tokens; only A-Z
uc.ExpressionTokens[TokenType.AlphaNumeric].Regex = "[a-zA-Z]+";

uc.DefineVariable("Other_Variable = 333");
Console.WriteLine(uc.Error.Message);
uc.DefineVariable("OtherVariable123 = 444");
Console.WriteLine(uc.Error.Message);

Console.WriteLine(uc.EvalStr("Other_Variable"));
Console.WriteLine(uc.EvalStr("OtherVariable123 "));
Console.WriteLine(uc.EvalStr("My_Variable"));
Console.WriteLine(uc.EvalStr("Variable123"));
Console.WriteLine("---");

// We restore the alphanumeric regex to support _ and numbers again
// Note: My_Variable and Variable123 remained; they were simply inaccessible
uc.ExpressionTokens[TokenType.AlphaNumeric].Regex = "[a-zA-Z_][a-zA-Z0-9_]*";
Console.WriteLine(uc.EvalStr("My_Variable"));
Console.WriteLine(uc.EvalStr("Variable123"));
				
			
No error
No error
[a-zA-Z_][a-zA-Z0-9_]*
111
222
---
Invalid definition
Invalid definition
Undefined identifier
Undefined identifier
Undefined identifier
Undefined identifier
---
111
222
				
					#include <iostream>
#include "uCalc.h"

using namespace std;
using namespace uCalcSoftware;

int main() {
   uCalc uc;
   // (See alternate version of this example using ItemOf instead of ExpressionTokens)

   // In this section underscore, _, and numeric digits
   // are accepted as part of alphanumeric tokens
   uc.DefineVariable("My_Variable = 111");
   cout << uc.Error().Message() << endl;
   uc.DefineVariable("Variable123 = 222");
   cout << uc.Error().Message() << endl;

   cout << uc.ExpressionTokens()[TokenType::AlphaNumeric].Regex() << endl;
   cout << uc.EvalStr("My_Variable") << endl;
   cout << uc.EvalStr("Variable123") << endl;
   cout << "---" << endl;

   // Now we no longer want underscore, _, or numeric digits
   // to be accepted in alphanumeric tokens; only A-Z
   uc.ExpressionTokens()[TokenType::AlphaNumeric].Regex("[a-zA-Z]+");

   uc.DefineVariable("Other_Variable = 333");
   cout << uc.Error().Message() << endl;
   uc.DefineVariable("OtherVariable123 = 444");
   cout << uc.Error().Message() << endl;

   cout << uc.EvalStr("Other_Variable") << endl;
   cout << uc.EvalStr("OtherVariable123 ") << endl;
   cout << uc.EvalStr("My_Variable") << endl;
   cout << uc.EvalStr("Variable123") << endl;
   cout << "---" << endl;

   // We restore the alphanumeric regex to support _ and numbers again
   // Note: My_Variable and Variable123 remained; they were simply inaccessible
   uc.ExpressionTokens()[TokenType::AlphaNumeric].Regex("[a-zA-Z_][a-zA-Z0-9_]*");
   cout << uc.EvalStr("My_Variable") << endl;
   cout << uc.EvalStr("Variable123") << endl;
}
				
			
No error
No error
[a-zA-Z_][a-zA-Z0-9_]*
111
222
---
Invalid definition
Invalid definition
Undefined identifier
Undefined identifier
Undefined identifier
Undefined identifier
---
111
222
				
					Imports System
Imports uCalcSoftware
Public Module Program
   Public Sub Main()
      Dim uc As New uCalc()
      '// (See alternate version of this example using ItemOf instead of ExpressionTokens)
      
      '// In this section underscore, _, and numeric digits
      '// are accepted as part of alphanumeric tokens
      uc.DefineVariable("My_Variable = 111")
      Console.WriteLine(uc.Error.Message)
      uc.DefineVariable("Variable123 = 222")
      Console.WriteLine(uc.Error.Message)
      
      Console.WriteLine(uc.ExpressionTokens(TokenType.AlphaNumeric).Regex)
      Console.WriteLine(uc.EvalStr("My_Variable"))
      Console.WriteLine(uc.EvalStr("Variable123"))
      Console.WriteLine("---")
      
      '// Now we no longer want underscore, _, or numeric digits
      '// to be accepted in alphanumeric tokens; only A-Z
      uc.ExpressionTokens(TokenType.AlphaNumeric).Regex = "[a-zA-Z]+"
      
      uc.DefineVariable("Other_Variable = 333")
      Console.WriteLine(uc.Error.Message)
      uc.DefineVariable("OtherVariable123 = 444")
      Console.WriteLine(uc.Error.Message)
      
      Console.WriteLine(uc.EvalStr("Other_Variable"))
      Console.WriteLine(uc.EvalStr("OtherVariable123 "))
      Console.WriteLine(uc.EvalStr("My_Variable"))
      Console.WriteLine(uc.EvalStr("Variable123"))
      Console.WriteLine("---")
      
      '// We restore the alphanumeric regex to support _ and numbers again
      '// Note: My_Variable and Variable123 remained; they were simply inaccessible
      uc.ExpressionTokens(TokenType.AlphaNumeric).Regex = "[a-zA-Z_][a-zA-Z0-9_]*"
      Console.WriteLine(uc.EvalStr("My_Variable"))
      Console.WriteLine(uc.EvalStr("Variable123"))
   End Sub
End Module
				
			
No error
No error
[a-zA-Z_][a-zA-Z0-9_]*
111
222
---
Invalid definition
Invalid definition
Undefined identifier
Undefined identifier
Undefined identifier
Undefined identifier
---
111
222
Token(TokenType)

ID: 170

				
					using uCalcSoftware;

var uc = new uCalc();
Console.WriteLine(uc.ExpressionTokens[TokenType.AlphaNumeric].Name);
Console.WriteLine(uc.ExpressionTokens[TokenType.AlphaNumeric].Regex);
Console.WriteLine("");

// Note: In C# or VB you can simply use [TokenType::Literal, n]
// instead of ByType(TokenType::Literal, n)
Console.WriteLine(uc.ExpressionTokens.ByType(TokenType.Literal, 0).Name);
Console.WriteLine(uc.ExpressionTokens.ByType(TokenType.Literal, 1).Name);
Console.WriteLine(uc.ExpressionTokens.ByType(TokenType.Literal, 2).Name);
				
			
_token_alphanumeric
[a-zA-Z_][a-zA-Z0-9_]*

_token_string_singlequoted
_token_string_doublequoted
_token_string_tripledoublequoted
				
					#include <iostream>
#include "uCalc.h"

using namespace std;
using namespace uCalcSoftware;

int main() {
   uCalc uc;
   cout << uc.ExpressionTokens()[TokenType::AlphaNumeric].Name() << endl;
   cout << uc.ExpressionTokens()[TokenType::AlphaNumeric].Regex() << endl;
   cout << "" << endl;

   // Note: In C# or VB you can simply use [TokenType::Literal, n]
   // instead of ByType(TokenType::Literal, n)
   cout << uc.ExpressionTokens().ByType(TokenType::Literal, 0).Name() << endl;
   cout << uc.ExpressionTokens().ByType(TokenType::Literal, 1).Name() << endl;
   cout << uc.ExpressionTokens().ByType(TokenType::Literal, 2).Name() << endl;
}
				
			
_token_alphanumeric
[a-zA-Z_][a-zA-Z0-9_]*

_token_string_singlequoted
_token_string_doublequoted
_token_string_tripledoublequoted
				
					Imports System
Imports uCalcSoftware
Public Module Program
   Public Sub Main()
      Dim uc As New uCalc()
      Console.WriteLine(uc.ExpressionTokens(TokenType.AlphaNumeric).Name)
      Console.WriteLine(uc.ExpressionTokens(TokenType.AlphaNumeric).Regex)
      Console.WriteLine("")
      
      '// Note: In C# or VB you can simply use [TokenType::Literal, n]
      '// instead of ByType(TokenType::Literal, n)
      Console.WriteLine(uc.ExpressionTokens.ByType(TokenType.Literal, 0).Name)
      Console.WriteLine(uc.ExpressionTokens.ByType(TokenType.Literal, 1).Name)
      Console.WriteLine(uc.ExpressionTokens.ByType(TokenType.Literal, 2).Name)
   End Sub
End Module
				
			
_token_alphanumeric
[a-zA-Z_][a-zA-Z0-9_]*

_token_string_singlequoted
_token_string_doublequoted
_token_string_tripledoublequoted