File size: 930 Bytes
1d96a60
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6bf62af
 
 
 
 
 
1d96a60
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import re
from typing import Optional


def extract_verification_code(text: str) -> Optional[str]:
    """提取验证码"""
    if not text:
        return None

    # 策略1: 上下文关键词匹配(中英文冒号)
    context_pattern = r"(?:验证码|code|verification|passcode|pin).*?[::]\s*([A-Za-z0-9]{4,8})\b"
    match = re.search(context_pattern, text, re.IGNORECASE)
    if match:
        candidate = match.group(1)
        # 排除 CSS 单位值
        if not re.match(r"^\d+(?:px|pt|em|rem|vh|vw|%)$", candidate, re.IGNORECASE):
            return candidate

    # 策略2: 6位字母数字混合(与测试代码一致,优先级提高)
    match = re.search(r"[A-Z0-9]{6}", text)
    if match:
        return match.group(0)

    # 策略3: 6位数字(降级为备选)
    digits = re.findall(r"\b\d{6}\b", text)
    if digits:
        return digits[0]

    return None