Regex - remove everything before and include the current criteria

Viewed 58

Im trying to create a pattern and to remove everything before and including the patter if possilble.

My sample text is:

Building configuration...

Current configuration : 32546 bytes
!
! Last configuration change at 22:48:59 UTC Wed Jan 6 2021 by bobb
! NVRAM config last updated at 22:35:10 UTC Mon Nov 2 2020 by johns
!
version 16.10

Id like anything before version removing, I didn't mind the first ! so I have the below which gets the NVRAM line

^! NVRAM.+

now what would my python/regex look like to remove that NVRAM line and everything before it?

Thanks for any help

1 Answers

You can use

\A(?:(?!! NVRAM).*\n)*! NVRAM.*\n*
\A(?:[\s\S]*?\n)?! NVRAM.*\n*

See the regex demo #1 and regex demo #2.

Details:

  • \A - start of string
  • (?:(?!! NVRAM).*\n)* - zero or more sequences of lines that do not start with ! NVRAM
  • (?:[\s\S]*?\n)? - an optional sequence (it is necessary if the line starting with ! NVRAM is the first line) any zero or more chars, as few as possible, up to the first newline char that is followed with the subsequent subpatterns
  • ! NVRAM.* - ! NVRAM and the rest of the line
  • \n* - optional zero or more newlines.
Related