Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Regex- How to capture everything up until another named capture group

I have the following text:

J.smith (2022-05-02 01:22:02) Hi There,

How are you doing today

Just wanted to check in

Bobby123 (2022-05-02 07:39:00):Hello ,

Im doing good thank you for asking

Thanks!

I want to have three named capture groups of Name, Time, and Text:

Bobby123

2022-05-02 01:22:02

Hello ,

Im doing good thank you for asking

Thanks!

My main issue is trying to create a named capture group for text. Every regex I’ve tried captures everything for the text, but the text group should stop after it reaches the second named capture group (name) of Bobby123.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Here’s what I have so far:

(?<by>([\S]+)) \((?<time>\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2})\)\: 

https://regex101.com/r/VCIUH9/1

>Solution :

You can use

(?<by>\S+) \((?<time>\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2})\):\s*(?<text>.*(?:\n(?!\S+ \(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\):).*)*)

See the regex demo.

Details:

  • (?<by>\S+) – Group "by": one or more non-whitespaces
  • \( – a space and (
  • (?<time>\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}) – a datetime pattern
  • \): – a ) and :
  • \s* – zero or more whitespaces
  • (?<text>.*(?:\n(?!\S+ \(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\):).*)*) – Group "text":
    • .* – the rest of the line
    • (?:\n(?!\S+ \(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\):).*)* – zero or more lines that do not start with any word, one space, a datetime pattern and then ): string.
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading