1

This is the sample:

YYYY-MM-DD HH:II:SS Message Log
  Message Log
  Message Log

YYYY-MM-DD HH:II:SS Message Log

I want to get all characters after the timestamp from the first line including all characters of the succeeding line until the blank line?

This is the expected output:

YYYY-MM-DD HH:II:SS Message Log
  Message Log
  Message Log

I've tried doing something like this:

/^(\d{4}\-\d{2}\-\d{2} \d{2}\:\d{2}\:\d{2}) ([^\n]*)/s

The first group is the timestamp then the second group I am trying to get all characters including all characters of the succeeding line until the blank line

ElGavilan
  • 5,747
  • 16
  • 24
  • 35
Nataraki
  • 97
  • 1
  • 10
  • I wouldn't use regex for that. What language are you using? – Maroun Jan 28 '15 at 12:10
  • I am using this for nxlog for a condition. Here's what I was actually doing: regex: `/^(\d{4}\-\d{2}\-\d{2} \d{2}\:\d{2}\:\d{2}) ([^\n]*)/s` The first group is to capture the timestamp, then in the second group that's the part where I want to get all characters after the timestamp including all characters of the succeeding line until the blank line. – Nataraki Jan 28 '15 at 12:50
  • @MVF If your regex flavor supports lookarounds try: `^(\d{4}\-\d{2}\-\d{2} \d{2}\:\d{2}\:\d{2}) ([\s\S]*?)(?=\n\n|$)` else `^(\d{4}\-\d{2}\-\d{2} \d{2}\:\d{2}\:\d{2}) ([\s\S]*?)(\n\n|$)` or [test at regex101](https://regex101.com/r/pM9yO9/3) – Jonny 5 Jan 28 '15 at 12:56
  • @Jonny5 I tried your `^(\d{4}\-\d{2}\-\d{2} \d{2}\:\d{2}\:\d{2}) ([\s\S]*?)(?=\n\n|$)` and `^(\d{4}\-\d{2}\-\d{2} \d{2}\:\d{2}\:\d{2}) ([\s\S]*?)(\n\n|$)` but in my json file I see that it captures the all here's the output: `"Message Log\r\n Message Log\r\n Message Log\rn\rnYYYY-MM-DD HH:II:SS Message Log"` – Nataraki Jan 28 '15 at 13:05
  • @MVF try to replace the `\n\n` part with `\r?\n\r?\n` – Jonny 5 Jan 28 '15 at 13:08
  • @Jonny5 You nailed it! `^(\d{4}\-\d{2}\-\d{2} \d{2}\:\d{2}\:\d{2}) ([\s\S]*?)(?=\r?\n\r?\n|$)` – Nataraki Jan 28 '15 at 13:22
  • Glad you got it going @MVF :) – Jonny 5 Jan 28 '15 at 13:22

2 Answers2

4
^[\s\S]*?(?=\n{2,}|$)

Try this.See demo.

https://regex101.com/r/pM9yO9/2

vks
  • 63,206
  • 9
  • 78
  • 110
  • Here's what I was actually trying to do: `/^(\d{4}\-\d{2}\-\d{2} \d{2}\:\d{2}\:\d{2},\d{3}) ([^\n]*)/s` The first group is to capture the timestamp, then in the second group that's the part where I want to get all characters after the timestamp including all characters of the succeeding line until the blank line. – Nataraki Jan 28 '15 at 12:39
  • 1
    @Jonny5 really??ur welcome....do keep `Pune` in ur itinerary.... how do you know chai ? :P – vks Jan 28 '15 at 13:01
  • I love good chai. In Darjeeling had a few cups already few years ago :] – Jonny 5 Jan 28 '15 at 13:04
  • 1
    @Jonny5 Ohh!!!!!!!!dats cool!!!!!!!!! chai + cigarette...dats a routine here for us......just like having water :P – vks Jan 28 '15 at 13:06
1

How about:

(\d{4}\-\d{2}\-\d{2} \d{2}\:\d{2}\:\d{2})([\s\S]+?)\n\n

The lines you want are in group 2, the date/time in group 1.

Toto
  • 83,193
  • 59
  • 77
  • 109
  • Here's what I was actually trying to do: `/^(\d{4}\-\d{2}\-\d{2} \d{2}\:\d{2}\:\d{2},\d{3}) ([^\n]*)/s` The first group is to capture the timestamp, then in the second group that's the part where I want to get all characters after the timestamp including all characters of the succeeding line until the blank line. – Nataraki Jan 28 '15 at 12:39
  • @Jonny sorry it should be `/^(\d{4}\-\d{2}\-\d{2} \d{2}\:\d{2}\:\d{2}) ([^\n]*)/s` – Nataraki Jan 28 '15 at 12:51
  • did you missed the `)` for group 1? – Nataraki Jan 28 '15 at 13:12
  • @MVF: Yes, sorry I typed a `(` instead of a `)`. – Toto Jan 28 '15 at 13:15
  • @MVF: What doesn't work? What language or editor are you using? May be you have to replace `\n\n` by `\R\R`. – Toto Jan 28 '15 at 13:24
  • nope I am using it in nxlog regex condition this is what I did to your code `if $raw_event =~ /(\d{4}\-\d{2}\-\d{2} \d{2}\:\d{2}\:\d{2})([\s\S]+?)\n\n/` – Nataraki Jan 28 '15 at 13:28