But you want the space to be preceded by at least 10 non-newlines:s/\s/\n/g;
But this doesn't work, because the newlines we put in aren't found by the lookbehind, so we're going to have to consume and replace characters instead of using lookbehind:s/(?<=[^\n]{10})\s/\n/g;
I'm still using [^\n] because I assume that if the string already has embedded newlines, you want the counter to reset.s/([^\n]{10})\s/$1\n/g;
Ok, bonus round: there is a way to do it with lookbehind, but it's ugly. We use \G to indicate where the last match left off. We want there to be at least 10 non-newlines between any new match and the old one. So:
The lookbehind is "10 non-newlines, and be sure that none of them is immediately preceded by the previous match".s/(?<=(?:(?<!\G)[^\n]){10})\s/\n/g;
Update: Ok, re-reading the original question, we don't want to SUBSTITUTE for spaces, we want the newline to be inserted AFTER a space, and the space is (at least) the 10th character, meaning it's preceded by 9 chars. Thus:
(thanks to ambrus)s/([^\n]{9}\s)/$1\n/g;
In reply to Re: Counting characters in a string
by Roy Johnson
in thread Counting characters in a string
by TASdvlper
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |