用于匹配冒号和下一个冒号之前的最后一个换行符的正则表达式

Regex for matching between a colon and last newline prior to next colon

我正在尝试使用正则表达式解析字符串以提取冒号和下一个冒号之前的最后一个换行符之间的信息。我该怎么做?

string <- "Name: Al's\nPlace\nCountry:\nState\n/ Province: RI\n"
stringr::str_extract_all(string, "(?<=:)(.*)(?:\n)")

但我得到:

[[1]]
[1] " Al's\n" " \n"  " RI\n" 

当我想要的时候:

[[1]]
[1] " Al's\nPlace\n" " \n"  " RI\n" 

我不确定这是否是您想要的,因为您想要的输出看起来有点不同。

:((?:.*\n?)+?)(?=.*:|$)
  • : 匹配冒号
  • ((?:.*\n?)+?) 匹配和 capture lazily 任何行(可选 \n
  • (?=.*:|$) 直到有冒号的那一行 ahead

See this demo at regex101