如何替换字符串的3rd+匹配项?我希望在Dart中保留第一行和第二行,但将所有其他行替换为空字符串,但问题本身与语言无关
我想改信
a
b
c
d
e
f至
a
b
cdef发布于 2021-05-11 07:34:36
使用以下内容:
const s = 'a\nb\nc\nd\ne\nf'
const chunks = s.split('\n')
console.log( [chunks[0], chunks[1], chunks.slice(2).join("") ].join("\n"))
首先用换行符拆分,然后取前两项,并用换行符合并其余项。
发布于 2021-05-01 20:41:48
这可能不是最好的解决方案,但它是有效的:
void main(List<String> arguments) {
final input = '''
a
b
c
d
e
f
''';
final output = input.replaceFirstMapped(
RegExp(r"(([^\n]+\n){2})((.|\n)+)"),
(m) => "${m.group(1)!}${m.group(3)!.replaceAll("\n", "")}",
);
print(output);
}https://stackoverflow.com/questions/67346085
复制相似问题