正则表达式,用于在一行开头捕获N个单词而不影响下一行

时间:2016-01-08 00:20:58

标签: regex notepad++

我有一个大的csv文件,我需要捕获一行(6或更少)的前几个单词。我在记事本++中使用替换,我有这个正则表达式。

^((\w+\S+)\s+){1,6}.*$     (My 'replace with' text is \1...)

我的问题是,如果一行少于6个单词,它将影响下一行。

例如:如果我在此文本上运行替换:

one two three four five six
one two three four
one two three four five six
one two three four five six

我得到的是这个结果:

one two three four five six...
one two three four
one two...
one two three four five six...

这是我追求的结果:

one two three four five six...
one two three four...
one two three four five six...
one two three four five six...

非常感谢任何帮助。

2 个答案:

答案 0 :(得分:2)

\s包含换行符。请改为(?:(?!\n)\s)[^\S\n]

^((\w+\S+)[^\S\n]+){1,6}.*$

此外,这包括以下空格,因此不会匹配具有正好六个单词的行。试试这个:

^((?:\w+\S*)(?:[^\S\n]+(\w+\S*)){0,5}).*$

答案 1 :(得分:1)

我做:

找到:^((?:\S+\h){6}).*$
替换为:$1

这将删除第六个单词后出现的所有内容。少于6个单词的行将保留原样。

\h代表水平空格。
\w+\S+可以缩减为\S+,如果它不符合您的需求,请保留\w+\S+

确保您没有选中dot matches newline

相关问题