提取手机号(模仿)
使用正则表达式从文本中提取所有 11 位手机号。
查看参考答案 ▼
import re
text = "联系人A: 13800138000, 联系人B: 13900139000, 编号: A1001"
phones = re.findall(r"\\d{11}", text)
print(phones)
✓ 正确
参考答案:import re
text = "联系人A: 13800138000, 联系人B: 13900139000, 编号: A1001"
phones = re.findall(r"\\d{11}", text)
print(phones)
创建学生表(模仿)
创建 student 表,包含 id(主键自增)、name(非空字符串)、age(整数)、score(decimal)。
查看参考答案 ▼
CREATE TABLE student (
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(50) NOT NULL,
age INT,
score DECIMAL(5, 2)
);
✓ 正确
参考答案:CREATE TABLE student (
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(50) NOT NULL,
age INT,
score DECIMAL(5, 2)
);
查询指定列(模仿)
从 product 表中查询商品名称 name 和价格 price。
查看参考答案 ▼
SELECT name, price FROM product;
✓ 正确
参考答案:SELECT name, price FROM product;
提取订单号(变体)
从文本中提取所有以 P 开头、后面跟 4 位数字的订单号。
查看参考答案 ▼
import re
text = "订单号: P1001, P2030, X999, P8888"
orders = re.findall(r"P\\d{4}", text)
print(orders)
✓ 正确
参考答案:import re
text = "订单号: P1001, P2030, X999, P8888"
orders = re.findall(r"P\\d{4}", text)
print(orders)
完整校验手机号(变体)
判断列表中哪些是完整合法的 11 位手机号格式。
查看参考答案 ▼
import re
phones = ["13800138000", "电话13800138000", "13800138000123", "13900139000"]
for phone in phones:
if re.match(r"^1\\d{10}$", phone):
print(phone)
✓ 正确
参考答案:import re
phones = ["13800138000", "电话13800138000", "13800138000123", "13900139000"]
for phone in phones:
if re.match(r"^1\\d{10}$", phone):
print(phone)
RAG 文档清洗(综合)
提取手机号、删除页码、内部标记、页脚,压缩重复空行。
查看参考答案 ▼
import re
raw = <span class="str">"""第 1 页\
【内部资料】课程答疑记录\
\
学生手机号:13800138000\
问题:re.match 和 re.search 有什么区别?\
\
\
页脚:仅供学习"""</span>
phones = re.findall(r"\\d{11}", raw)
cleaned = re.sub(r"第\\s*\\d+\\s*页", "", raw)
cleaned = re.sub(r"【.*?】", "", cleaned)
cleaned = re.sub(r"页脚:.*", "", cleaned)
cleaned = re.sub(r"\
{2,}", "\
", cleaned).strip()
print(phones)
print(cleaned)
✓ 正确
参考答案:import re
raw = <span class="str">"""第 1 页\
【内部资料】课程答疑记录\
\
学生手机号:13800138000\
问题:re.match 和 re.search 有什么区别?\
\
\
页脚:仅供学习"""</span>
phones = re.findall(r"\\d{11}", raw)
cleaned = re.sub(r"第\\s*\\d+\\s*页", "", raw)
cleaned = re.sub(r"【.*?】", "", cleaned)
cleaned = re.sub(r"页脚:.*", "", cleaned)
cleaned = re.sub(r"\
{2,}", "\
", cleaned).strip()
print(phones)
print(cleaned)
电商商品数据查询(综合)
基于 category 和 product 表完成:1.商品名称+价格+分类名;2.价格>平均价的商品;3.每个分类商品数量;4.库存最低的前3个商品。
查看参考答案 ▼
-- 1. 商品名称、价格、分类名称(INNER JOIN)
SELECT p.name, p.price, c.name FROM product p INNER JOIN category c ON p.category_id = c.id;
-- 2. 价格高于平均价的商品(子查询)
SELECT * FROM product WHERE price > (SELECT AVG(price) FROM product);
-- 3. 每个分类商品数量(GROUP BY)
SELECT category_id, COUNT(*) AS product_count FROM product GROUP BY category_id;
-- 4. 库存最低的前3个商品(ORDER BY + LIMIT)
SELECT * FROM product ORDER BY stock ASC LIMIT 0, 3;
✓ 正确
参考答案:-- 1. 商品名称、价格、分类名称(INNER JOIN)
SELECT p.name, p.price, c.name FROM product p INNER JOIN category c ON p.category_id = c.id;
-- 2. 价格高于平均价的商品(子查询)
SELECT * FROM product WHERE price > (SELECT AVG(price) FROM product);
-- 3. 每个分类商品数量(GROUP BY)
SELECT category_id, COUNT(*) AS product_count FROM product GROUP BY category_id;
-- 4. 库存最低的前3个商品(ORDER BY + LIMIT)
SELECT * FROM product ORDER BY stock ASC LIMIT 0, 3;