蓝色天空.知识 · 实践 · 创造
我的收藏
在线代码实验室

离线校验 Scala Regex 查找、替换、分组提取与字符类量词

PYTHONWindows x64、CPython 3.12.14 离线 Scala 2.13/3 语义验证器(本机未安装 scala/scalac)。explore-regular-expressions.py
explore-regular-expressions.py117 行
"""Offline Scala semantic verification lab for Chapter 22 (regular-expressions)."""
import json
import re

VERSION = "CPython 3.12.14 offline Scala 2.13/3 semantic verifier (scalac absent)"


def case_find_first_and_find_all_mkstring():
    m1 = re.search(r"Scala", "Scala is Scalable and cool")
    first_opt = f"Some({m1.group(0)})" if m1 else "None"
    all_matches = [m.group(0) for m in re.finditer(r"(?:S|s)cala", "Scala is scalable and cool")]
    expected = {
        "find_first": "Some(Scala)",
        "find_all_mkstring": "Scala,scala",
    }
    actual = {
        "find_first": first_opt,
        "find_all_mkstring": ",".join(all_matches),
    }
    return {
        "name": "find_first_and_find_all_mkstring",
        "summary": "验证 Regex.findFirstIn 返回 Option 与 findAllIn.mkString(\",\") 拼接全部匹配项",
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def case_replace_first_and_replace_all():
    pat = re.compile(r"(?:S|s)cala")
    src = "Scala is scalable and cool"
    rep_first = pat.sub("Java", src, count=1)
    rep_all = pat.sub("Java", src)
    expected = {
        "replace_first": "Java is scalable and cool",
        "replace_all": "Java is Javable and cool",
    }
    actual = {
        "replace_first": rep_first,
        "replace_all": rep_all,
    }
    return {
        "name": "replace_first_and_replace_all",
        "summary": "验证 replaceFirstIn 仅替换首个匹配项与 replaceAllIn 替换全部匹配项的差异",
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def case_regex_quantifiers_groups_and_escapes():
    # Source example: new Regex("abl[ae]\\d+") on "ablaw is able1 and cool"
    abl_matches = re.findall(r"abl[ae]\d+", "ablaw is able1 and cool")
    # Capturing group extraction (equivalent to Scala Regex unapplySeq)
    date_m = re.fullmatch(r"(\d{4})-(\d{2})-(\d{2})", "2026-12-01")
    non_cap = re.findall(r"(?:[Rr]uby)", "Ruby and ruby")
    quoted_lit = re.search(re.escape("(a+b)*3"), "calc (a+b)*3 done")
    expected = {
        "abl_digits": ["able1"],
        "date_groups": ("2026", "12", "01"),
        "non_capturing": ["Ruby", "ruby"],
        "literal_quote_match": "(a+b)*3",
    }
    actual = {
        "abl_digits": abl_matches,
        "date_groups": date_m.groups() if date_m else (),
        "non_capturing": non_cap,
        "literal_quote_match": quoted_lit.group(0) if quoted_lit else "",
    }
    return {
        "name": "regex_quantifiers_groups_and_escapes",
        "summary": "验证字符类+量词(abl[ae]\\d+)、捕获/非捕获分组以及字面量转义(\\Q...\\E 等价)行为",
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def case_char_classes_and_word_boundaries():
    d3 = re.findall(r"\b\d{3}\b", "12 345 6789 007")
    d3_5 = re.findall(r"\b\d{3,5}\b", "12 345 6789 123456")
    ruby_opt = [bool(re.fullmatch(r"ruby?", s)) for s in ("rub", "ruby", "rubyy")]
    neg_vowels = re.findall(r"[^aeiou]", "plain")
    expected = {
        "exact_3_digits": ["345", "007"],
        "range_3_5_digits": ["345", "6789"],
        "ruby_optional_y": [True, True, False],
        "non_vowels_in_plain": ["p", "l", "n"],
    }
    actual = {
        "exact_3_digits": d3,
        "range_3_5_digits": d3_5,
        "ruby_optional_y": ruby_opt,
        "non_vowels_in_plain": neg_vowels,
    }
    return {
        "name": "char_classes_and_word_boundaries",
        "summary": "验证 \\d/\\b 单词边界、{n}/{n,m} 重复次数、? 可选量词与反向字符集 [^aeiou]",
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def main():
    cases = [
        case_find_first_and_find_all_mkstring(),
        case_replace_first_and_replace_all(),
        case_regex_quantifiers_groups_and_escapes(),
        case_char_classes_and_word_boundaries(),
    ]
    print(json.dumps({"version": VERSION, "cases": cases}, ensure_ascii=False, indent=2))


if __name__ == "__main__":
    main()
本例需在 Windows x64、CPython 3.12.14 离线 Scala 2.13/3 语义验证器(本机未安装 scala/scalac)。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程

运行环境:Windows x64、CPython 3.12.14 离线 Scala 2.13/3 语义验证器(本机未安装 scala/scalac)。

入口文件:projects/scala-regular-expressions-lab/explore-regular-expressions.py

本地运行步骤快速上手
  1. 点击上方“保存源码”保存为 explore-regular-expressions.py,或下载右侧课程工程包。
  2. 确认本地已准备 Windows x64、CPython 3.12.14 离线 Scala 2.13/3 语义验证器(本机未安装 scala/scalac)。。
  3. 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载

下载课程关联工程包:Scala

课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2428.zip

下载课程关联工程包:Scala

课程关联工程包(登记关联25章) · 含当前章节或实例 · mbskycom202612a2409.zip
SITE SEARCH

搜索教程、工具与资讯

热门搜索

JavaScriptJSON格式化正则表达式AI 编程AI 翻译AI导航专题标签
Esc 关闭

欢迎登录蓝色天空网

使用会员账号,继续你的学习