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?
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
CodePudding user response:
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:
Match literally(
Capture group 2.*
Match the rest of the line(?:
Non capture group\R
Match any unicode newline sequence(?![^:\n\r] :[^:\n\r]*$)
Assert that the line does not contain a single occurrence of:
.*
Match the whole line
)*
Close the non capture group and optionally repeat it to match all lines
)
Close group 2
See a regex demo and a PHP demo.