#include "demo_guard.h"
void regex_row(String label, String result, String expect)
{
bool pass = (result == expect);
<>
| = label ?> |
= result ?> |
">= pass ? "pass" : "expected: " + expect ?> |
>
}
void regex_bool_row(String label, bool result, bool expect)
{
regex_row(label, result ? "true" : "false", expect ? "true" : "false");
}
RENDER(Request& context)
{
String text = "Contact ops@example.test or tag #uce, #docs, and café.";
DValue first_email = regex_search("(?[A-Za-z0-9._%+-]+)@(?[A-Za-z0-9.-]+)", text);
DValue tags = regex_search_all("#(?[A-Za-z0-9_]+)", text);
StringList pieces = regex_split("\\s*,\\s*", "uce, components, markdown");
<>
UCE Test:
Regular Expressions
UCE regex functions use PCRE2 and return ordinary UCE strings, lists, and DValue values.
sample = = text ?>
Validation And Search
regex_bool_row("regex_match(\"[A-Z][a-z]+\", \"Alice\")", regex_match("[A-Z][a-z]+", "Alice"), true);
regex_bool_row("regex_match(\"[A-Z][a-z]+\", \"Alice!\")", regex_match("[A-Z][a-z]+", "Alice!"), false);
regex_bool_row("regex_search(\"example\", text)[\"matched\"]", first_email["matched"].to_bool(), true);
regex_row("first_email[\"match\"]", first_email["match"].to_string(), "ops@example.test");
regex_row("first_email[\"named\"][\"user\"]", first_email["named"]["user"].to_string(), "ops");
regex_row("first_email[\"named\"][\"host\"]", first_email["named"]["host"].to_string(), "example.test");
regex_bool_row("regex_match(\"\\\\p{L}+\", \"café\")", regex_match("\\p{L}+", "café"), true);
?>
All Matches
regex_row("regex_search_all hashtags count", tags["count"].to_string(), "2.000000");
regex_row("first hashtag", tags["matches"]["0"]["named"]["tag"].to_string(), "uce");
regex_row("second hashtag", tags["matches"]["1"]["named"]["tag"].to_string(), "docs");
?>
Replace And Split
regex_row("regex_replace(\"#([A-Za-z0-9_]+)\", \"$1\", \"#uce\")", regex_replace("#([A-Za-z0-9_]+)", "$1", "#uce"), "uce");
regex_row("join(regex_split(\"\\\\s*,\\\\s*\", ...), \"|\")", join(pieces, "|"), "uce|components|markdown");
regex_row("case-insensitive flag", regex_search("uce", "Hello UCE", "i")["match"].to_string(), "UCE");
?>
Structured Result
= json_encode(first_email) ?>
Read the regex API documentation
>
}