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("大括号未闭合") |