使用正则表达式每n行拆分一个字符串

时间:2014-10-20 06:44:34

标签: javascript regex string

"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."]

2 个答案:

答案 0 :(得分:1)

首先,您不想在此处使用split(),因为您需要一个对此具有完全lookaround assertion支持的正则表达式引擎。幸运的是,.match()也可以做到这一点(可以说更好):

result = subject.match(/(?:^.*$\n?){1,3}/mg);

测试live on regex101.com

<强>解释

(?:    # 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)

蒂姆,这是一个多么美好,干净的答案。我需要一个Python版本,这似乎削减了它:

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.']

确实确实如此!