I want to match only the nth (let's say 5th) whitespace character on a line and then replace that space with a line break (so that all my lines essentially have no more than 4 words / 4 spaces). On the REPLACE line in Notepad++, I think an \r\n would generally do the trick, and it is just the FIND pattern that is giving me problems.
I know there is a relatively simple regex solution for this that I am blanking on, but in my searches of "Find Nth instance of pattern on a line" type regex answers, the regex that has been closest to what I want is also matching everything before that 5th whitespace on a line. Here is that code that is grabbing too much.
^(?:\S+\s){5}
I know the "\S+" probably should not be there, but my attempts to pair this code down to some basic variations on this below
^(\s){5}
have all failed one way or another.
So here is what the text result of the regex should be.
FROM:
This line is too long. I want to shorten it.
This line is too long. I would really like to know why my regex is not working.
TO:
This line is too long.
I want to shorten it.
This line is too long.
I would really like to
know why my regex is
not working.
答案 0 :(得分:1)
Use the following regex in find:
((?:\S+\s){4}\S+)\s
Then replace it with \1\r\n
.
This essentially captures everything up to the 5th space and then replaces it with the first capture group (\1
) followed by \r\n
.
答案 1 :(得分:0)
如果你没有保留现有的换行符,那么一个简单的解决方案就是做两个单独的替换:
首先,使用单个空格搜索\s+
和全部替换。
然后做这个替换:
找到:
(\S+ \S+ \S+ \S+ \S+)\s
用“全部替换”:
$1\r\n