[英]Regex matching multiple lines multiple times without matching date and time
Regex matching multiple lines multiple times 正则表达式多次匹配多行
\s*([^:]+?)\s*:\s*(.*(?:\s*(?!.*:).*)*)\s*
This solution matches the date and time string.此解决方案匹配日期和时间字符串。
How do I modify the above solution so that the date and time is included with the Description: header?如何修改上述解决方案,使日期和时间包含在描述中:header?
Name: John Doe
Age: 23
Primary Language: English
Description: This is a multiline
description field that I want
to capture Fri, 02 Sep 2022 14:46:45 -0500
Country: Canada
One option could be to exclude matching a comma in the first part before the colon:一个选项可能是在冒号前的第一部分排除匹配逗号:
^([^,:\n\r]+):(.*(?:\R(?![^,:\n\r]+:).*)*)
Another option could be asserting that the next lines to match do not contain only a single colon:另一种选择可能是断言要匹配的下一行不只包含一个冒号:
^([^:\n\r]+):(.*(?:\R(?![^:\n\r]+:[^:\n\r]*$).*)*)
Explanation解释
^
Start of string ^
字符串开头([^:\n\r]+)
Capture group 1 , match 1+ chars other than :
or a newline ([^:\n\r]+)
捕获组 1 ,匹配除:
或换行符以外的 1+ 个字符:
Match literally :
从字面上匹配(
Capture group 2 (
捕获组 2
.*
Match the rest of the line .*
匹配线的rest(?:
Non capture group (?:
非捕获组
\R
Match any unicode newline sequence \R
匹配任何 unicode 换行序列(?:[^:\n\r]+:[^:\n\r]*$)
Assert that the line does not contain a single occurrence of :
(?:[^:\n\r]+:[^:\n\r]*$)
断言该行不包含单次出现的:
.*
Match the whole line .*
匹配整行)*
Close the non capture group and optionally repeat it to match all lines )*
关闭非捕获组并可选择重复它以匹配所有行)
Close group 2 )
关闭第 2 组See a regex demo and a PHP demo .请参阅正则表达式演示和PHP 演示。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.