"LINE 1.
LINE 2.
LINE 3.
LINE 4.
LINE 5.
LINE 6."
假设我想使用split()
方法每3行拆分上面的字符串,我应该使用什么正则表达式分隔符来生成这样的内容:
["LINE 1.
LINE 2.
LINE 3.",
"LINE 4.
LINE 5.
LINE 6."]
答案 0 :(得分:1)
首先,您不想在此处使用split()
,因为您需要一个对此具有完全lookaround assertion支持的正则表达式引擎。幸运的是,.match()
也可以做到这一点(可以说更好):
result = subject.match(/(?:^.*$\n?){1,3}/mg);
<强>解释强>
(?: # 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.
答案 1 :(得分:0)
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))
给出结果
['LINE 1.\nLINE 2.\nLINE 3.\n', 'LINE 4.\nLINE 5.\nLINE 6.']
用4而不是3来代替
['LINE 1.\nLINE 2.\nLINE 3.\nLINE 4.\n', 'LINE 5.\nLINE 6.']
确实确实如此!