| 1234567891011121314151617181920212223242526272829303132333435363738394041 |
- import re
- import requests
- # 获取common模板
- resp = requests.get("http://localhost:8000/api/templates/x11_wash_common")
- common_body = resp.json()["template_body"]
- print("=== Common模板前200字符 ===")
- print(common_body[:200])
- # 测试宏提取 - 使用DOTALL和MULTILINE
- text_sample = '''{% macro count_round(value) %}
- {% set value = value | int %}
- {{ (value / 10) | round(0) | int * 10 }}
- {% endmacro %}
- {% macro approx_fraction(p) -%}
- {%- set p = 99 if p |int >= 100 else p -%}
- {%- endmacro %}
- '''
- macro_pattern = r'{%-?\s*macro\s+\w+\s*\([^)]*\)\s*-%?%}[\s\S]*?{%-?\s*endmacro\s*-%?%}'
- print("\n=== 测试文本样本 ===")
- test_matches = list(re.finditer(macro_pattern, text_sample))
- print(f"测试样本宏匹配数量: {len(test_matches)}")
- for i, m in enumerate(test_matches):
- print(f"匹配 {i+1}: {m.group(0)[:80]}...")
- print("\n=== 实际common模板 ===")
- matches = list(re.finditer(macro_pattern, common_body))
- print(f"宏匹配数量: {len(matches)}")
- for i, m in enumerate(matches):
- print(f"匹配 {i+1}: {m.group(0)[:80]}...")
- # 测试列表变量提取
- list_set_pattern = r'{%\s*set\s+(greeting|assistant1|assistant2|user1|user2|period_list|city_list)\s*=\s*\[[^\]]*\]\s*%}'
- list_matches = list(re.finditer(list_set_pattern, common_body, re.IGNORECASE))
- print(f"\n列表变量匹配数量: {len(list_matches)}")
- for m in list_matches:
- print(f"变量: {m.group(0)[:60]}...")
|