reckfellENGINE

RegEx

Compile a PCRE2 pattern, search and replace text.

On this page
class RegExReckfellSharpInherits RefCounted

Compile and check Error.Ok before Search/SearchAll/Sub. Reuse a successfully compiled object for repeated searches. CreateFromString is convenient, but still requires IsValid checking.

Patterns pass through C# string-literal processing and then PCRE2 syntax. For example, @"\d+" and "\\d+" supply the same pattern. An online test using another regex engine does not establish equivalent behavior.

This example finds the first run of ASCII digits and extracts its named group. It works after emoji/Cyrillic without passing native indices to C# Substring.

C#
using Reckfell;

public static class RegExTextExample
{
    public static bool TryFindNumber(string text, out string number)
    {
        number = "";
        using var expression = new RegEx();
        if (expression.Compile(@"(?<number>[0-9]+)") != Error.Ok)
            return false;
        using var match = expression.Search(text);
        if (match is null)
            return false;
        number = match.GetString("number");
        return true;
    }
}

RegEx positions use native 32-bit string units. For valid Unicode these are codepoints; C# uses UTF-16, while visible graphemes can contain multiple codepoints. Do not mix these indices directly. This API exposes no search-timeout parameter; successful compilation does not promise bounded time for every input.

Declarations checked against C# SDK 1.0.0. Expand a name to see declarations and documentation.

Constructors1

RegExConstructor
# RegEx
C#
public RegEx();

Creates an object with no compiled pattern. Before Search, SearchAll, Sub or group metadata queries, call Compile and check Error.Ok/IsValid.

Methods12

Clearvoid
# Clear
C#
public void Clear();

Releases compiled code and makes IsValid false. GetPattern retains its text: not a complete reset to a fresh object. Previously obtained RegExMatch data remains available.

Compile2 overloads
# Compile · 1/2
C#
public Error Compile(string pattern);

Compiles a PCRE2 pattern and replaces the current one. Old code is discarded before the attempt: failure does not retain a usable previous pattern, while GetPattern stores the new attempted text. The overload without showError uses true.

Parameters
NameTypeDefault
patternstring—
pattern

PCRE2 pattern after C# string-literal processing. For example, @"\d+" and "\\d+" supply the same pattern. Do not assume automatic .NET Regex syntax equivalence.

Returns Error

Error.Ok on success; Error.Failed when compilation fails.

# Compile · 2/2
C#
public Error Compile(string pattern, bool showError = true);

Compiles a PCRE2 pattern and replaces the current one. Old code is discarded before the attempt: failure does not retain a usable previous pattern, while GetPattern stores the new attempted text. The overload without showError uses true.

Parameters
NameTypeDefault
patternstring—
showErrorbooltrue
pattern

PCRE2 pattern after C# string-literal processing. For example, @"\d+" and "\\d+" supply the same pattern. Do not assume automatic .NET Regex syntax equivalence.

showError

Whether to log compilation errors. false suppresses the message, but neither validates a pattern nor changes the Error result.

Returns Error

Error.Ok on success; Error.Failed when compilation fails.

CreateFromString2 overloads
# CreateFromString · 1/2
C#
public static RegEx CreateFromString(string pattern);

Creates an object and calls Compile. A failed compilation still returns a RegEx object: check IsValid, not merely non-null. Use instance Compile when you need a separate Error.

Parameters
NameTypeDefault
patternstring—
pattern

PCRE2 pattern after C# string-literal processing. For example, @"\d+" and "\\d+" supply the same pattern. Do not assume automatic .NET Regex syntax equivalence.

Returns RegEx

New RegEx; an invalid pattern can produce an invalid object with a non-null reference.

# CreateFromString · 2/2
C#
public static RegEx CreateFromString(string pattern, bool showError = true);

Creates an object and calls Compile. A failed compilation still returns a RegEx object: check IsValid, not merely non-null. Use instance Compile when you need a separate Error.

Parameters
NameTypeDefault
patternstring—
showErrorbooltrue
pattern

PCRE2 pattern after C# string-literal processing. For example, @"\d+" and "\\d+" supply the same pattern. Do not assume automatic .NET Regex syntax equivalence.

showError

Whether to log compilation errors. false suppresses the message, but neither validates a pattern nor changes the Error result.

Returns RegEx

New RegEx; an invalid pattern can produce an invalid object with a non-null reference.

GetGroupCountint
# GetGroupCount
C#
public int GetGroupCount();

Returns the compiled pattern capture-group count, excluding whole-match group 0. Requires a valid pattern; not the number of matches in text.

Returns int

Group count excluding group 0; check IsValid first.

GetNamesstring[]
# GetNames
C#
public string[] GetNames();

Returns pattern capture-group names. An array position is neither the group number nor a promised parenthesis order. Use RegExMatch.Names for a particular match.

Returns string[]

string[] of names, not a group-number dictionary.

GetPatternstring
# GetPattern
C#
public string GetPattern();

Returns the latest Compile input, including a failed attempt. Clear does not erase this string; nonempty text does not establish IsValid.

Returns string

Retained pattern text, which may have no usable compiled code.

IsValidbool
# IsValid
C#
public bool IsValid();

Checks whether compiled code exists. true does not mean that a particular subject matches or establish a search deadline.

Returns bool

true when compiled code exists; false for a new/cleared object or after failed compilation.

SearchRegExMatch
# Search
C#
public RegExMatch Search(string subject, int offset = 0, int end = -1);

Finds the first match within the original subject bounds. offset does not rebase the ^ anchor; prior context remains available to boundary checks. Result positions refer to original subject in native 32-bit units, not C# UTF-16 indices.

Parameters
NameTypeDefault
subjectstring—
offsetint0
endint-1
subject

Original text to search/replace. Native codepoint indices for valid Unicode can differ from C# string.Length/Substring indices after non-BMP characters.

offset

Zero-based start in native 32-bit units; must be >= 0. Not a byte offset or UTF-16 index.

end

Exclusive right bound, not a length. A negative value, including default -1, means through the end; a bound past the end does not extend subject. Same units as offset.

Returns RegExMatch

RegExMatch or null. null can mean no match or search failure; the SDK signature does not expose this nullable possibility.

SearchAllReckfell.Collections.Array<RegExMatch>
# SearchAll
C#
public Reckfell.Collections.Array<RegExMatch> SearchAll(string subject, int offset = 0, int end = -1);

Returns nonoverlapping matches with positions in the original subject. After a zero-length match, the implementation advances the next search start by one native unit. Elements are RegExMatch objects, not strings.

Parameters
NameTypeDefault
subjectstring—
offsetint0
endint-1
subject

Original text to search/replace. Native codepoint indices for valid Unicode can differ from C# string.Length/Substring indices after non-BMP characters.

offset

Zero-based start in native 32-bit units; must be >= 0. Not a byte offset or UTF-16 index.

end

Exclusive right bound, not a length. A negative value, including default -1, means through the end; a bound past the end does not extend subject. Same units as offset.

Returns Reckfell.Collections.Array<RegExMatch>

Typed Array<RegExMatch>; empty when no matches are found.

Substring
# Sub
C#
public string Sub(string subject, string replacement, bool all = false, int offset = 0, int end = -1);

Replaces the first or all matches. replacement is parsed as a replacement template with $1/$name references, not arbitrary literal text. Bounds refer to original subject; outside prefix and suffix are preserved. Requires a valid compiled pattern.

Parameters
NameTypeDefault
subjectstring—
replacementstring—
allboolfalse
offsetint0
endint-1
subject

Original text to search/replace. Native codepoint indices for valid Unicode can differ from C# string.Length/Substring indices after non-BMP characters.

replacement

Replacement template; $1 and $name reference captured groups. Literal user text can require separate escaping for replacement syntax.

all

false replaces the first match; true replaces all applicable matches within the bounds.

offset

Zero-based start in native 32-bit units; must be >= 0. Not a byte offset or UTF-16 index.

end

Exclusive right bound, not a length. A negative value, including default -1, means through the end; a bound past the end does not extend subject. Same units as offset.

Returns string

Complete output text on successful substitution. PCRE2 failures can produce empty output and diagnostics, rather than a separate Error result.

Reckfell Engine documentationBack to top ↑
QUICK LINKSGetting startedEntity hierarchyC# & .NET 10Scenes and prefabs
Reckfell guides and full API K
reckfellENGINE
RECKFELL ACCOUNT

Sign in

Sign in to your Reckfell account.

One account for the entire Reckfell platform.