uCalc API Version: 2.1.3-preview.2 Released: 6/16/2026

Warning

uCalc API Preview Release Notice:The documentation describes the intended behavior of the API. The current preview build contains incomplete features, unoptimized performance, and is subject to breaking changes.

uCalc Patterns for Common Regex Tasks (A Cookbook)

Product: 

Class: 

A practical guide showing how to solve common text-processing problems using uCalc's token-aware patterns as a powerful alternative to Regex.

Remarks

🍳 uCalc Cookbook: Regex to uCalc Patterns

For developers accustomed to Regular Expressions (Regex), switching to a new pattern-matching syntax can be a hurdle. This cookbook is designed to bridge that gap. It presents common text-processing problems and shows how to solve them using both traditional Regex and uCalc's powerful, token-aware patterns.

While Regex is a fantastic tool for character-level matching, you'll see how uCalc's approach often leads to patterns that are safer, more readable, and better at handling structured data.


1. Matching Whole Words

The Goal: Find the word is but not as a substring inside This.

  • Regex Solution: \bis\bYou must explicitly add word boundaries (\b) to prevent partial matches.

  • uCalc Solution: isuCalc's engine is token-aware. It breaks the input into words first. The pattern is will only match the token is, not the characters i and s inside the token This.


2. Matching Any Number

The Goal: Find integers, decimals, and scientific notation (e.g., 10, 3.14, -1.5e-2).

  • Regex Solution: [-+]?\d*\.?\d+([eE][-+]?\d+)?This is complex, hard to read, and may not cover all formats.

  • uCalc Solution: {@Number}The {@Number} category matcher is a simple, readable instruction. The uCalc tokenizer handles the complex logic of identifying all valid number formats internally. If you need to support a new format (like hexadecimal 0x...), you only need to update the token definition, not every pattern that uses it.


3. Matching Content Inside Quotes

The Goal: Extract the text from within double quotes, correctly handling escaped quotes.

  • Regex Solution: "(.*?)" (using a non-greedy quantifier) or "((?:\\"|[^"])*)" (more robust, but very complex).

  • uCalc Solution: {@String:content}The {@String} category matcher leverages the tokenizer, which understands string literals as atomic units. It automatically and safely handles escaped quotes and other complexities.


4. Extracting Key-Value Pairs

The Goal: Parse key = value; where whitespace is variable.

  • Regex Solution: \s*(\w+)\s*=\s*([^;]*);You must manually account for optional whitespace (\s*) around the key, equals sign, and value.

  • uCalc Solution: {@Alpha:key} = {value};uCalc is whitespace-insensitive by default. A single space in the pattern matches any amount of horizontal whitespace (or none) in the source text, making the pattern clean and readable.


5. Matching Balanced Delimiters

The Goal: Match a complete HTML-style tag, like <b>content</b>, but reject mismatched tags like <b>content</i>.

  • Regex Solution: <(\w+)>(.*?)<\/\1>This uses a numeric backreference (\1) to ensure the closing tag matches the captured opening tag. It's functional but can become confusing with multiple capture groups.

  • uCalc Solution: <{tag}>{content}</{tag}>uCalc uses simple, named backreferences. By reusing the variable name {tag}, the engine automatically enforces that the captured text must be identical in both places. This is far more readable and maintainable.


6. Splitting a String by a Delimiter

The Goal: Split a comma-separated list, but correctly handle delimiters that appear inside quoted strings.

  • Language Solution: myString.Split(',')A simple split will incorrectly break up a,"b,c",d into a, "b, c", d.

  • uCalc Solution: Use a Transformer to find each element.The pattern {!ArgSeparator:item} captures any token that is not a comma. The tokenizer's default QuoteSensitive behavior ensures that "b,c" is treated as a single string token, so the pattern correctly captures it as one element.


7. Validating an Email Address (Hybrid Approach)

The Goal: Check if a string looks like a valid email address.

  • Regex Solution: A very long and famously complex regex.

  • uCalc Hybrid Solution: {'[\w.-]+'}@{'[\w.-]+'}\.{'[a-zA-Z]{2,}'}This pattern shows uCalc's flexibility. While the overall structure (user@domain.tld) is defined with tokens, we can embed short, simple regex snippets inside single quotes for character-level validation where it makes sense. This gives you the best of both worlds: high-level structural awareness and low-level precision.

Examples

Extracts all numbers from a string, demonstrating the simplicity of the `{@Number}` category matcher.
				
					using uCalcSoftware;

var uc = new uCalc();
using (var t = new uCalc.Transformer()) {
   t.FromTo("{@Number:n}", "[NUM:{n}]");

   var text = "Order 123 has 2 items for 49.95 total.";
   Console.WriteLine(t.Transform(text));
};
				
			
Order [NUM:123] has [NUM:2] items for [NUM:49.95] total.
				
					#include <iostream>
#include "uCalc.h"

using namespace std;
using namespace uCalcSoftware;

int main() {
   uCalc uc;
   {
      uCalc::Transformer t;
      t.Owned(); // Causes t to be released when it goes out of scope
      t.FromTo("{@Number:n}", "[NUM:{n}]");

      auto text = "Order 123 has 2 items for 49.95 total.";
      cout << t.Transform(text) << endl;
   };
}
				
			
Order [NUM:123] has [NUM:2] items for [NUM:49.95] total.
				
					Imports System
Imports uCalcSoftware
Public Module Program
   Public Sub Main()
      Dim uc As New uCalc()
      Using t As New uCalc.Transformer()
         t.FromTo("{@Number:n}", "[NUM:{n}]")
         
         Dim text = "Order 123 has 2 items for 49.95 total."
         Console.WriteLine(t.Transform(text))
      End Using
   End Sub
End Module
				
			
Order [NUM:123] has [NUM:2] items for [NUM:49.95] total.