File size: 814 Bytes
63c6bf0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import re

text = "\\boxed{\\left(3.0, \\frac{\\pi}{2}\\right)}\n\\"
start = text.find(r"\boxed{")
if start == -1:
    print("未找到 \boxed{}")
else:
    start += len(r"\boxed{")  # 跳过 "\boxed{" 的起始位置
    brace_level = 1
    end = start
    while end < len(text) and brace_level > 0:
        if text[end] == "{":
            brace_level += 1
        elif text[end] == "}":
            brace_level -= 1
        end += 1

    if brace_level == 0:
        full_match = text[start-7:end]  # 包含 "\boxed{" 和末尾 "}"
        inner_content = text[start:end-1]  # 内部内容(去掉末尾的 "}")
        print(full_match)  # 输出:\boxed{\left(3.0, \frac{\pi}{2}\right)}
        print(inner_content)  # 输出:\left(3.0, \frac{\pi}{2}\right)
    else:
        print("大括号未闭合")