Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Limit number of lines in text by a regular expression

Tags:

regex

In any programming language I know how to efficiently limit the number of lines in a given file or string, that is not the issue here. However in this case I am looking to do this by a regular expression. In this pattern I am using only \n newline characters. I have no need for others such as \r carriage returns.

(?:(?:\n)?[^\n]*){0,3}

The above regular expression explained:

(?:       group, but do not capture (between 0 and 3 times)-
 (?:      group, but do not capture (optional)
  \n      '\n' (newline)
 )?       end of grouping
 [^\n]*   any character except: '\n' (newline) (0 or more times)
){0,3}    end of grouping

Now using this regular expression in a string such as..

In this line is foo bar and baz
In this line is bar and foo
In this line is baz and bar
In this line we have foo
In this line we have bar and foo and baz
In this line we have foobar
In this line we have foo
In this line we have foo and bar
In this line we have bar and baz and foo

This will return lines 1-3 with no issue.

In the above string, lines 7, 8, and 9 all contain the word foo all by itself whether it is in the beginning, the middle or the end of the string.

Now my question is how could I implement either a look ahead or look behind to search a string and find 3 lines of text in a row that all have that same keyword foo by itself and not as a prefix of a word or combined in another word? So therefore it would match only lines 7-9 and not 1-6.

like image 705
hwnd Avatar asked Dec 11 '25 08:12

hwnd


1 Answers

I don't see why this would need any kind of lookaround. Just match only lines that contain foo:

(?:\n?[^\n]*foo[^\n]*){3}

Notice that with the optional \n this might as well match a line that contains foo three times. To avoid that, use

(?:(?:^|\n)[^\n]*foo[^\n]*){3}
// or
(?:[^\n]*foo[^\n]*(?:\n|$)){3}

(depending on your regex flavour you might use different anchors for string beginning/end)


If you need foo standing on it's own, just add word boundaries to it:

(?:\n?[^\n]*\bfoo\b[^\n]*){3}
like image 91
Bergi Avatar answered Dec 13 '25 21:12

Bergi



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!