python正则表达式回车

2024-04-25 22:21:33 发布

您现在位置:Python中文网/ 问答频道 /正文

请你帮我用正则表达式把所有的东西都放到“;”上好吗

window.egfunc = {
  name: "test name",
  type: "test type",
  storeId: "12345"
};

我有以下工作时,所有的数据将在一行,但一旦有回报,它不会工作

window.egfunc\s+=\s+(.*);

Tags: 数据nametesttypewindowstoreidegfunc
2条回答

.匹配除换行符以外的所有字符\n, \r。一旦遇到任何LF角色,它就会停止。我们必须明确地告诉regex使用.作为多行,使用标志re.DOTALL

其他语言中也有类似的修饰符,比如perl的s需要在最后添加

import re

txt = """window.egfunc = {
  name: "test name",
  type: "test type",
  storeId: "12345"
};"""

x = re.findall("window.egfunc\s+=\s+(.*);", txt, re.DOTALL)
print(x)

还有一个re.MULTILINEre.M,但它不能单独工作。如果有人知道原因,你能评论一下吗

将点替换为[\s\S]

window.egfunc\s+=\s+([\s\S]*?);

相关问题 更多 >