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

运行 SQLite 3.53 %/_ 通配符、自定义 REGEXP 与原生 GLOB 字符类交叉实测

PYTHONWindows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。explore-wildcards.py
explore-wildcards.py156 行
"""SQL 通配符离线实验:验证 %、_ 与字符集模式(SQLite 自定义 REGEXP 函数及原生 GLOB)。"""
import json
import platform
import re
import sqlite3


def run() -> dict:
    conn = sqlite3.connect(":memory:")
    conn.create_function(
        "REGEXP",
        2,
        lambda pattern, value: bool(re.search(pattern, value)) if value is not None else False,
    )
    conn.executescript(
        """
        CREATE TABLE websites (
            id INTEGER PRIMARY KEY,
            name TEXT NOT NULL,
            url TEXT NOT NULL,
            alexa INTEGER NOT NULL,
            country TEXT NOT NULL
        );
        INSERT INTO websites VALUES
            (1, 'Google', 'https://www.google.com/', 1, 'USA'),
            (2, '淘宝', 'https://www.taobao.com/', 13, 'CN'),
            (3, '蓝色天空', 'http://www.mbsky.com/', 5000, 'USA'),
            (4, '微博', 'http://weibo.com/', 20, 'CN'),
            (5, 'Facebook', 'https://www.facebook.com/', 3, 'USA'),
            (7, 'stackoverflow', 'http://stackoverflow.com/', 0, 'IND');
        """
    )
    cases = []

    # Case 1: % wildcard on url
    https_sites = [
        r[0]
        for r in conn.execute(
            "SELECT name FROM websites WHERE url LIKE 'https%' ORDER BY id;"
        ).fetchall()
    ]
    oo_urls = [
        r[0]
        for r in conn.execute(
            "SELECT name FROM websites WHERE url LIKE '%oo%' ORDER BY id;"
        ).fetchall()
    ]
    exp1 = {
        "url_https": ["Google", "淘宝", "Facebook"],
        "url_contains_oo": ["Google", "Facebook"],
    }
    act1 = {"url_https": https_sites, "url_contains_oo": oo_urls}
    cases.append(
        {
            "name": "wildcard_percent_url_patterns",
            "summary": "使用 % 通配符筛选 url 以 https 开头及包含 oo 的网站记录",
            "expected": exp1,
            "actual": act1,
            "passed": act1 == exp1,
        }
    )

    # Case 2: _ single-character wildcard on name
    under1 = [
        r[0]
        for r in conn.execute(
            "SELECT name FROM websites WHERE name LIKE '_oogle' ORDER BY id;"
        ).fetchall()
    ]
    under2 = [
        r[0]
        for r in conn.execute(
            "SELECT name FROM websites WHERE name LIKE 'G_o_le' ORDER BY id;"
        ).fetchall()
    ]
    exp2 = {"pattern_oogle": ["Google"], "pattern_G_o_le": ["Google"]}
    act2 = {"pattern_oogle": under1, "pattern_G_o_le": under2}
    cases.append(
        {
            "name": "wildcard_underscore_single_char",
            "summary": "使用 _ 单字符通配符验证 '_oogle' 与 'G_o_le' 精准定位 Google",
            "expected": exp2,
            "actual": act2,
            "passed": act2 == exp2,
        }
    )

    # Case 3: Character classes via REGEXP UDF and native SQLite GLOB
    re_gfs = [
        r[0]
        for r in conn.execute(
            "SELECT name FROM websites WHERE name REGEXP '^[GFs]' ORDER BY id;"
        ).fetchall()
    ]
    re_ah = [
        r[0]
        for r in conn.execute(
            "SELECT name FROM websites WHERE name REGEXP '^[A-H]' ORDER BY id;"
        ).fetchall()
    ]
    re_not_ah = [
        r[0]
        for r in conn.execute(
            "SELECT name FROM websites WHERE name REGEXP '^[^A-H]' ORDER BY id;"
        ).fetchall()
    ]
    glob_gfs = [
        r[0]
        for r in conn.execute(
            "SELECT name FROM websites WHERE name GLOB '[GFs]*' ORDER BY id;"
        ).fetchall()
    ]
    glob_ah = [
        r[0]
        for r in conn.execute(
            "SELECT name FROM websites WHERE name GLOB '[A-H]*' ORDER BY id;"
        ).fetchall()
    ]
    glob_not_ah = [
        r[0]
        for r in conn.execute(
            "SELECT name FROM websites WHERE name GLOB '[^A-H]*' ORDER BY id;"
        ).fetchall()
    ]
    exp3 = {
        "regexp_GFs": ["Google", "Facebook", "stackoverflow"],
        "regexp_A_H": ["Google", "Facebook"],
        "regexp_not_A_H": ["淘宝", "蓝色天空", "微博", "stackoverflow"],
        "glob_matches_regexp": True,
    }
    act3 = {
        "regexp_GFs": re_gfs,
        "regexp_A_H": re_ah,
        "regexp_not_A_H": re_not_ah,
        "glob_matches_regexp": (re_gfs == glob_gfs and re_ah == glob_ah and re_not_ah == glob_not_ah),
    }
    cases.append(
        {
            "name": "wildcard_charlist_regexp_and_glob",
            "summary": "通过 SQLite 注册 REGEXP 与原生 GLOB 验证 ^[GFs]、^[A-H] 与 ^[^A-H] 字符集合匹配",
            "expected": exp3,
            "actual": act3,
            "passed": act3 == exp3,
        }
    )

    conn.close()
    return {
        "version": f"CPython {platform.python_version()} / SQLite {sqlite3.sqlite_version}",
        "cases": cases,
    }


if __name__ == "__main__":
    print(json.dumps(run(), ensure_ascii=False, indent=2))
本例需在 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程

运行环境:Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。

入口文件:projects/sql-wildcards-lab/explore-wildcards.py

本地运行步骤快速上手
  1. 点击上方“保存源码”保存为 explore-wildcards.py,或下载右侧课程工程包。
  2. 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
  3. 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载

下载课程关联工程包:SQL

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

下载课程关联工程包:SQL

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

搜索教程、工具与资讯

热门搜索

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

欢迎登录蓝色天空网

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