RegEx
Compile a PCRE2 pattern, search and replace text.
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.
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
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
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
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.
| Name | Type | Default |
|---|---|---|
pattern | string | — |
patternPCRE2 pattern after C# string-literal processing. For example, @"\d+" and "\\d+" supply the same pattern. Do not assume automatic .NET Regex syntax equivalence.
ErrorError.Ok on success; Error.Failed when compilation fails.
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.
| Name | Type | Default |
|---|---|---|
pattern | string | — |
showError | bool | true |
patternPCRE2 pattern after C# string-literal processing. For example, @"\d+" and "\\d+" supply the same pattern. Do not assume automatic .NET Regex syntax equivalence.
showErrorWhether to log compilation errors. false suppresses the message, but neither validates a pattern nor changes the Error result.
ErrorError.Ok on success; Error.Failed when compilation fails.
CreateFromString2 overloads
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.
| Name | Type | Default |
|---|---|---|
pattern | string | — |
patternPCRE2 pattern after C# string-literal processing. For example, @"\d+" and "\\d+" supply the same pattern. Do not assume automatic .NET Regex syntax equivalence.
RegExNew RegEx; an invalid pattern can produce an invalid object with a non-null reference.
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.
| Name | Type | Default |
|---|---|---|
pattern | string | — |
showError | bool | true |
patternPCRE2 pattern after C# string-literal processing. For example, @"\d+" and "\\d+" supply the same pattern. Do not assume automatic .NET Regex syntax equivalence.
showErrorWhether to log compilation errors. false suppresses the message, but neither validates a pattern nor changes the Error result.
RegExNew RegEx; an invalid pattern can produce an invalid object with a non-null reference.
GetGroupCountint
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.
intGroup count excluding group 0; check IsValid first.
GetNamesstring[]
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.
string[]string[] of names, not a group-number dictionary.
GetPatternstring
public string GetPattern();Returns the latest Compile input, including a failed attempt. Clear does not erase this string; nonempty text does not establish IsValid.
stringRetained pattern text, which may have no usable compiled code.
IsValidbool
public bool IsValid();Checks whether compiled code exists. true does not mean that a particular subject matches or establish a search deadline.
booltrue when compiled code exists; false for a new/cleared object or after failed compilation.
SearchRegExMatch
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.
| Name | Type | Default |
|---|---|---|
subject | string | — |
offset | int | 0 |
end | int | -1 |
subjectOriginal text to search/replace. Native codepoint indices for valid Unicode can differ from C# string.Length/Substring indices after non-BMP characters.
offsetZero-based start in native 32-bit units; must be >= 0. Not a byte offset or UTF-16 index.
endExclusive 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.
RegExMatchRegExMatch or null. null can mean no match or search failure; the SDK signature does not expose this nullable possibility.
SearchAllReckfell.Collections.Array<RegExMatch>
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.
| Name | Type | Default |
|---|---|---|
subject | string | — |
offset | int | 0 |
end | int | -1 |
subjectOriginal text to search/replace. Native codepoint indices for valid Unicode can differ from C# string.Length/Substring indices after non-BMP characters.
offsetZero-based start in native 32-bit units; must be >= 0. Not a byte offset or UTF-16 index.
endExclusive 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.
Reckfell.Collections.Array<RegExMatch>Typed Array<RegExMatch>; empty when no matches are found.
Substring
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.
| Name | Type | Default |
|---|---|---|
subject | string | — |
replacement | string | — |
all | bool | false |
offset | int | 0 |
end | int | -1 |
subjectOriginal text to search/replace. Native codepoint indices for valid Unicode can differ from C# string.Length/Substring indices after non-BMP characters.
replacementReplacement template; $1 and $name reference captured groups. Literal user text can require separate escaping for replacement syntax.
allfalse replaces the first match; true replaces all applicable matches within the bounds.
offsetZero-based start in native 32-bit units; must be >= 0. Not a byte offset or UTF-16 index.
endExclusive 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.
stringComplete output text on successful substitution. PCRE2 failures can produce empty output and diagnostics, rather than a separate Error result.