asmo
asmo

Reputation: 21

PCRE regex - how to capture first match from lines above the pattern?

I have following text in a file:

mysql_databases:
  potato_mychat1:
    - potato_mychat1
  potato_phpb1:
    - potato_phpb1
    - potato_smf1
  potato_phpb2:
    - potato_phpb2
  p_phpb282:
    - potato_phpb282
  potato_registry:
    - potato_registryadmi
  potato_smf1:
    - potato_smf1
  potato_smf2:
    - potato_smf2
  potato_wp82:
   - potato_wp82

The pattern would be - potato_smf1.

Te first string ending with : above each match should be marked - so in this case potato_phpb1: and potato_smf1:.

I have tried many variations of positive lookbehind: (?<= - potato_smf)*: but can't get it right no matter what, I will appreciate any hints.

Upvotes: 2

Views: 83

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626689

Assuming you can rely on indentation, you can use

(?m)^\h{2}\S.*(?=(?:\R\h{4}.*)*?\R\h{4}- potato_smf1)

See the regex demo

Details

  • (?m)^ - start of a line
  • \h{2} - two horizontal whitespaces
  • \S.* - a non-whitespace and then any 0 or more chars other than line break chars, as many as possible
  • (?=(?:\R\h{4}.*)*?\R\h{4}- potato_smf1) - a positive lookahead that requires, immediately on the right,
    • (?:\R\h{4}.*)*? - zero or more, but as few as possible, repetitions of a line break, four horizontal whitespaces and then zero or more chars other than line break chars, as many as possible
    • \R - a line break
    • \h{4} - four horizontal whitespaces
    • - potato_smf1 - a literal text.

Upvotes: 2

Related Questions