Multilinear, variedly greedy, regular expression

Given the following text, what PCRE regular expression would you use to highlight parts in bold?

00: 20314 lorem ipsum
  want this
  kryptonite

00: 02314 quux
  padding
  dont want this

00: 03124 foo
     neither this

00: 01324 foo
     but we want this
     stalagmite

00: 02134 tralala
     not this

00: 03124 bar foo
     and we want this
     kryptonite but not this (!)

00: 02134 foo bar
     and not this either

00:01234 dolor sit amet
     Eof

IOW, we want to extract sections that start with regular expressions with "^ 0" and end with "(kryptonite | stalagmite)".

He waved it a bit, discovering that he was a tough nut. TIA!

+4
4

: Lookahead (?sm) .

(?sm)^0(?:(?!^0).)*?(?:kryptonite|stalagmite)

Live Demo

+4

, .

 # (?ms)^0(?:(?!(?:^0|kryptonite|stalagmite)).)*(kryptonite|stalagmite)

 (?ms)
 ^ 0
 (?:
      (?!
           (?: ^ 0 | kryptonite | stalagmite )
      )
      . 
 )*
 ( kryptonite | stalagmite )
+3

, :

^0(?:\R(?!\R)|.)*?\b(?:kryptonite|stalagmite)\b


, ^0, kryptonite, stalagmite ( , ), \b .

(?:\R(?!\R)|.)*? - , . PCRE \R .

(?:      (?# start non-capturing group for repetition)
  \R     (?# match a newline character)
  (?!\R) (?# not followed by another newline)
 |       (?# OR)
  .      (?# match any character, except newline)
)*?      (?# lazily repeat this group)
+2
source

^ (00 :. *? (Kryptonite | stalagmite)) with s modifier

-1
source

All Articles