渲染zh-Hans

regex

Translation status: 简体中文 reader-locale proof. Term names and code fences follow the zh-Hans pack; supporting prose may still be English.

Compile 文本 patterns to regex via ↦ regex.

Syntax: <文本> ↦ regex

Category#

literal

Examples#

radix/corpus/literalia/regex.fab (canonical · conversio)#

Compile 文本 patterns to regex via ↦ regex.

# =============================================================================
# regex — Compile 文本 patterns to regex via ↦ regex.
# =============================================================================
#
# What this teaches:
#   • converting 文本 strings to compiled regex with `↦ regex`
#   • inline regex flags like `(?i)` for case-insensitive matching
#   • combining `§` template application with regex 变换 for dynamic patterns
#
# Common mistakes:
#   • Forgetting that ↦ regex requires a 文本 or 窄字串 source — 整数 and other types must convert to 文本 first.
#
# See also: 读取, 转换, 格式化
# =============================================================================

# literalia/regex — 文本 ↦ regex 转换
#
# GRAMMAR:
#   转换 :← stringLit '↦' 'regex'
#   templateExpr :← stringLit '(' exprList ')'   -- then ↦ regex on the 文本 result
#
# EXPECTED OUTPUT:
#   Regex pattern diagnostics for literals, inline flags, and a template-built path.
#
# BACKEND:
#   Cross-ref 读取/lege.fab for regex beside stdin reads.
#   Cross-ref 格式化/scriptum.fab for § template application.

main {
    const regex digits ← "\d+" ↦ regex
    print digits
    const regex word ← "(?i)\w+" ↦ regex
    print word

    # Template application → 文本, then 转换 to regex (dynamic path segments)
    const string tenant ← "acme"
    const regex homePath ← "/home/§/.*"(tenant) ↦ regex
    print homePath
}

Expected output:

\d+
(?i)\w+
/home/acme/.*