Reputation: 23
"LINE 1.
LINE 2.
LINE 3.
LINE 4.
LINE 5.
LINE 6."
Suppose i want to split the above string every 3 lines using the split()
method, what regex delimiter should i use to produce something like this :
["LINE 1.
LINE 2.
LINE 3.",
"LINE 4.
LINE 5.
LINE 6."]
Upvotes: 0
Views: 2064
Reputation: 535
What a nice, clean answer, Tim. I needed a Python version, and this seems to cut it:
lines = """LINE 1.
LINE 2.
LINE 3.
LINE 4.
LINE 5.
LINE 6."""
import re
print(re.compile("(?:^.*$\n?){1,3}",re.M).findall(lines))
Gives the result
['LINE 1.\nLINE 2.\nLINE 3.\n', 'LINE 4.\nLINE 5.\nLINE 6.']
Subsituting with 4 instead of 3 should give
['LINE 1.\nLINE 2.\nLINE 3.\nLINE 4.\n', 'LINE 5.\nLINE 6.']
And indeed it does!
Upvotes: 0
Reputation: 336128
First, you don't want to use split()
here because you would need a regex engine with full lookaround assertion support for that. Fortunately, .match()
can do this just as well (arguably even better):
result = subject.match(/(?:^.*$\n?){1,3}/mg);
Test it live on regex101.com.
Explanation:
(?: # Start a non-capturing group that matches...
^ # (from the start of a line)
.* # any number of non-newline characters
$ # (until the end of the line).
\n? # Then it matches a newline character, if present.
){1,3} # It repeats this three times. If there are less than three lines
# at the end of the string, it is content with matching two or one, as well.
Upvotes: 2