Home

Awesome

TinyRegex

Small and portable Regular Expression (regex) library written in BeefLang, based on https://github.com/kokke/tiny-regex-c.

Supports a subset of the syntax and semantics of the Python standard library implementation (the re module).

Notable features and omissions

API

This is the public API:

/// Indicates whether the regular expression finds a match in the input string.
/// @param regex The regular expression pattern to match.
/// @param text The string to search for a match.
public static bool IsMatch(StringView regex, StringView text);

/// Searches an input string for a substring that matches a regular expression pattern.
/// @param regex The regular expression pattern to match.
/// @param text The string to search for a match.
public static Result<Match, MatchError> Match(StringView regex, StringView text);

/// Lazily enumerates over all matches of a regular expression pattern.
/// @param regex The regular expression pattern to match.
/// @param text The string to search for a match.
public static MatchEnumerator Matches(StringView regex, StringView text);

/// Replaces the first string that matches a regular expression pattern with a specified replacement string.
/// @param regex The regular expression pattern to match.
/// @param text The string to search for a match and which will be modified.
/// @param replace The replacement string.
public static void Replace(StringView regex, String text, StringView replace);

/// Replaces the first string that matches a regular expression pattern using a custom function.
/// @param regex The regular expression pattern to match.
/// @param text The string to search for a match and which will be modified.
/// @param replaceFunc A custom function that modifies the match.
public static void Replace<TFunc>(StringView regex, String text, TFunc replaceFunc)
  where TFunc : delegate void(String match);

/// Replaces strings that match a regular expression pattern with a specified replacement string.
/// @param regex The regular expression pattern to match.
/// @param text The string to search for a match and which will be modified.
/// @param replace The replacement string.
public static void ReplaceAll(StringView regex, String text, StringView replace);

/// Replaces strings that match a regular expression pattern using a custom function.
/// @param regex The regular expression pattern to match.
/// @param text The string to search for a match and which will be modified.
/// @param replaceFunc A custom function that modifies each match.
public static void ReplaceAll<TFunc>(StringView regex, String text, TFunc replaceFunc)
  where TFunc : delegate void(String match);

Supported regex-operators

The following features / regex-operators are supported by this library.

Examples

Example of usage:

if (Regex.Match(@"[Hh]ello [Ww]orld\s*[!]?", "ahem.. 'hello world !' ..") case .Ok(let match))
{
  Console.WriteLine($"Matched '{match.Value}' at index {match.Index}, {match.Length} chars long.");
  // outputs: Matched 'hello world !' at index 8, 13 chars long.
}

String text = scope .("24h 0x043k 0x4384");
Regex.ReplaceAll("0x([0-9]{2}){1,2}", text,
  (match) => {
    int num = int.Parse(match);
    match.Clear();
    (++num).ToString(match);
  });
Console.WriteLine(text);
// outputs: 24h 53k 17285

For more usage examples I encourage you to look at the code in the Tests project.