Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex: In a group defined by boundries, only match if XX

Tags:

regex

Context: I'm trying to filter some logs I have, and I can't change the way they are generated. What they look like is basically:

Some text here AAA
Some text here One
Some More text here
******
Some text here BBB
Some text here Two
Some More text here
******
Some text here CCC
Some text here Three
Some More text here
******
Some text here BBB
Some text here Four
Some More text here
******
Some text here BBB
Some text here Five
Some More text here
******

And basically, every ****** delimits a new log event.

I'm trying to get a regex to match every block that resides between two ****** that contains, for example BBB. (I know the first one doesn't start with ******, but I'm fine with not matching that one for complexity sake)

What I tried to do was basically "Define a potential match with something delimited by ****** but doesn't have another ******in between", but I don't know how to write that. So in the aforementioned example, it would match the blocks that contains Two, Four and Five.

I tried multiple variations of things like: \*{6}(?!\*{6}).*BBB.*\*{6}

Any tips?

Edit: The regex version I'm using is PCRE (php) in Notepad++/VisualStudio and Regex101.com.

like image 419
Tipx Avatar asked Feb 04 '26 16:02

Tipx


1 Answers

You may use this regex in PCRE:

(?:\A|\*{6})(?:(?!\*{6}).)*?BBB.*?(?=\R\*{6})

RegEx Demo

RegEx Detail:

  • (?:\A|\*{6}): Match start or ******
  • (?:(?!\*{6}).)*?: Match 0 or more characters that don't have ****** ahead of them
  • BBB.*?: Match BBB followed by 0 or more characters
  • (?=\R\*{6}): Using lookahead assert that we have ****** at next position
like image 92
anubhava Avatar answered Feb 06 '26 06:02

anubhava



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!