import re,json,csv,io,datetime
results={}
results['unicode_digits']=re.findall(r'\d','١2')
results['ascii_digits']=re.findall(r'\d','١2',re.A)
results['unicode_word']=bool(re.fullmatch(r'\w+','天空'))
results['dollar_final_lf']=bool(re.search(r'a$','a\n'))
results['fullmatch_final_lf']=bool(re.fullmatch(r'a','a\n'))
results['dot_cr']=bool(re.fullmatch(r'.','\r'))
results['named']=re.fullmatch(r'(?P<word>\w+) (?P=word)','sky sky').groupdict()
results['date_replace']=re.sub(r'(\d{4})-(\d{2})-(\d{2})',r'\g<3>/\g<2>/\g<1>','2026-11-03')
results['conditional']=[bool(re.fullmatch(r'(<)?sky(?(1)>|$)',s)) for s in ['sky','<sky>','<sky']]
try:re.compile(r'(?<=v\d+\.)\d+');results['variable_behind']='accepted'
except re.error:results['variable_behind']='error'
try:datetime.date.fromisoformat('2026-02-30');results['invalid_calendar']='accepted'
except ValueError:results['invalid_calendar']='error'
results['csv']=next(csv.reader(io.StringIO('"blue,sky",learn')))
assert results=={'unicode_digits':['١','2'],'ascii_digits':['2'],'unicode_word':True,'dollar_final_lf':True,'fullmatch_final_lf':False,'dot_cr':True,'named':{'word':'sky'},'date_replace':'03/11/2026','conditional':[True,True,False],'variable_behind':'error','invalid_calendar':'error','csv':['blue,sky','learn']}
print(json.dumps(results,ensure_ascii=False))
本例需在 Edge156 / Node24;跨语言对照另注明Python3.12、PHP8.4 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:Edge156 / Node24;跨语言对照另注明Python3.12、PHP8.4
入口文件:projects/regexp-lab/python_compare.py
预期结果与验证条件观察提示
- Unicode数字١与2,ASCII仅2;尾LF search成功/fullmatch失败;命名重复词;日期替换03/11/2026;条件组真真假;变长后行和错误历法拒绝;CSV引号逗号保留
请结合实际环境逐项观察。
附件与关联工程包下载
课程关联工程包(登记关联14章) · 含当前章节或实例 · mbskycom202611a527.zip