-1

I am struggling write a RegEx to extract many blocks (with start and end delimiters) from a large logfile, but only where a certain string is in the block. I want the RegEx to run in the VSCode Find tool so I can copy the matching blocks into a new file.

Example data

begin
  stuff
end
begin
  this is thing
end
begin
  others
end
begin
  also thing
end
begin
  other stuff
end

I want to write a regular expression to extract a list only blocks that contain the string "thing".

Desired output

begin
  this is thing
end
begin
  also thing
end

The RegEx I have so far is

begin[\s\S\r]+?end

The above is splitting the log file up into blocks, but I can't figure out how to selectively return only blocks containing the string "thing".

Michael12345
  • 2,422
  • 3
  • 22
  • 39
  • Try it with `^begin\n.*thing.*\nend$`. Don't ask me why your question got closed as I don't really see the answer in the linked question. – Patrick Janser May 10 '22 at 12:31
  • If you want to do the opposite and remove every block not containing `thing` then you can do a regex search with `^begin\r?\n(?![^\n\r]*\bthing\b).*?\r?\nend(?:\r?\n|$)` and replace by nothing. This will remove all the blocks not containing the word `thing`. This will avoid having something like *anything* or *something* taken in consideration. This is solved with the `\b` for word **b**oundary. The `\r?\n` is to match either Windows or Linux line endings. Test it here: https://regex101.com/r/w34VSo/1 – Patrick Janser May 10 '22 at 12:51
  • 1
    This closed correctly. The answer is ``begin(?:(?!begin|end)[\w\W])*thing[\w\W]*?end`` ([demo](https://regex101.com/r/zTi1ns/2)). Just use a tempered greedy token – Wiktor Stribiżew May 10 '22 at 12:56

0 Answers0