我试图更新一些用私人语言编写的代码..。无论如何,问题是我试图使用python语言中的regex来匹配其余的代码,如示例中所示:
from re import sub
data = """
Go To --> BB125276 {
Scan:
#f
}
"""
#//////////////////////////////////////
schema = r"""
Go To --> (.+) {
\s*(.+)
}
"""
#//////////////////////////////////////
final_form = r"""
Goto {\1}
\2
"""
#//////////////////////////////////////
massar_code = sub(schema, final_form, data)
print(massar_code)但它不匹配换行符和空格..。我要它匹配一切,任何帮助!??
发布于 2022-03-01 23:43:02
您需要将re.DOTALL或re.S标志添加到re.sub( )中:
massar_code = sub(schema, final_form, data, flags=re.S)使用re.S或re.DOTALL:
使'.‘特殊字符匹配任何字符,包括换行符;没有此标志,“。将匹配除换行符以外的任何内容。
https://stackoverflow.com/questions/71315781
复制相似问题