Tuesday, 10 September 2013

Limit number of lines in text by a regular expression

Limit number of lines in text by a regular expression

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 and foo
This will return lines 1-3 with no issue.
In the above string, lines 4, 5, and 6 all contain the word foo 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, look behind,
or look around to search a string and find 3 lines of text in a row that
all have that same keyword foo? So therefore it would match only lines 4-6
and not 1 or 2.

No comments:

Post a Comment