简体   繁体   中英

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?

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]+:).*)*)

Regex demo

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM