Insert a string after each number of words with conditionsbrowsing

What I wan to accomplish is a little bit complicated.
I want to insert the string [NEWLINE] after every 44 characters length including spaces. But with respect the the string cannot be in the middle of a word, and in that case the string should go back to the space before the that word.

Example #oneStraight forward

Original text
As your advisor I am qualified to assist you in all matters related to rule civilization. If you don’t require my services dismiss me to attend to other matters.

Result
As your advisor I am qualified to assist you[NEWLINE]in all matters related to rule civilization.[NEWLINE]**If you don’t require my services dismiss me[NEWLINE]to attend to other matters.

I think that you could obtain what you want, by using a S/R, with regular expressions, but not exactly with the same template that you proposed !

Before giving you the simple search and replacement regexes, needed, and the explanation of the slight differences with your seeked results, that produces this S/R, I would like to recapitulate your two examples, as I did not find, exactly, the same results as you ;-))

IMPORTANT :

Throughout all this post, I simply replaced the leading and trailing spaces, of the final lines of text, by the Double Low Line Unicode character ‗ ( \x{2017} ). It’s more readable and rigorous, isn’t it ?

The first block of the Correct Result 1 is 45 characters long ( not 44 ), due to the final space character

The first block of the Wanted Result 2 is 37 character long ( not 36 ), for the same reason !

Now, I think that the behaviour, about the slicing that you seek for, cannot be easily achieved with regular expressions, as the insertion of the End of Line character(s), inside text, would depend on the character right after the end of the next block of 44 characters ! I did try to find a suitable regex, without success, unfortunately :-((

But I’ve got an other solution which works out the current block to match, in order that it is always followed by a NON-word character, in the 44 characters limit !

The searched regex is, simply : (?-s).{16,44}(?=\W). You probably think : what the 16 number is for ?

Well, from the link below, it happens that the longest non-coined and non-technical English word is the word antidisestablishmentarianism, which is 28 characters long !! ( and 16 is just equal to 44 - 28 )

As the string 1234567890123456 antidisestablishmentarianism is 45 characters long ( so more than the limit of 44 chars), the regex engine backtracks, searching, successively, from beginning of that text, for a string of 43 characters long, followed by a non-word character, then for a string of 42 characters long, followed by a non-word character … and so on, till it detects a first match : the string 1234567890123456, of 16 characters long, which is followed with a space character

A second search gets all the remaining characters, as the string ‗antidisestablishmentarianism 12345678901234 is just 44 characters long

Now, let’s suppose you add the digit 5 at the end of the example text, as below :

1234567890123456 antidisestablishmentarianism 123456789012345

This time, again, as the second resulting string would be 45 characters long, the regex engine backtracks and selects, only, the string ‗antidisestablishmentarianism ( 29 characters long ) as a second match. Finally, the third match is the 16 characters long string ‗123456789012345 !

Applying my S/R, to your two examples, we obtain :

# Original text 1
As your advisor I am qualified to assist you in all matters related to rule civilization. If you don’t require my services dismiss me to attend to other matters.
# Final text 1
As your advisor I am qualified to assist you 44
‗in all matters related to rule civilization 44
. If you don’t require my services dismiss 42
‗me to attend to other matters. 31
# Original text 2
Greetings My Liege! As your personal advisor, I am qualified to assist you in all matters related to ruling our civilization. I am at your service.
# Final text 2
Greetings My Liege! As your personal advisor 44
, I am qualified to assist you in all 37
‗matters related to ruling our civilization. 44
‗I am at your service. 22

What do you think about this kind of slicing ? I do hope it could be OK for you :-))

The (?-s) part is a modifier, that forces the regex engine to consider the dot meta-character as matching a single standard character, only ( NOT the \r and the \n characters )

Then the .{16,44} part tries to match the longest string, containing between 16 to 44 characters, included

With the additional condition that the character, following that string, must be a NON-word character, due to the positive look-ahead syntax (?=\W)

In replacement, we, first, rewrite the entire searched string $0, followed by the string \r\n, which inserts the two Windows End of Line characters

In case, you’re using Unix files, note that the replacement zone should be $0\n, only

Finally, from the original paragraph 5 of the license.txt file of N++ v7.3.3, below :

5. You are not required to accept this License, since you have not signed it. However, nothing else grants you permission to modify or distribute the Program or its derivative works. These actions are prohibited by law if you do not accept this License. Therefore, by modifying or distributing the Program (or any work based on the Program), you indicate your acceptance of this License to do so, and all its terms and conditions for copying, distributing or modifying the Program or works based on it.

We would obtain, after replacement :

5. You are not required to accept this
‗License, since you have not signed it.
‗However, nothing else grants you permission
‗to modify or distribute the Program or its
‗derivative works. These actions are
‗prohibited by law if you do not accept this
‗License. Therefore, by modifying or
‗distributing the Program (or any work based
‗on the Program), you indicate your
‗acceptance of this License to do so, and
‗all its terms and conditions for copying,
‗distributing or modifying the Program or
‗works based on it.

Just notice that the space character ( usual word separator ) is always moved, with that S/R, at beginning of the next line. You may prefer the other text arrangement, below, where the non-word character is simply the last character of each final line, using the regex .{16,43}\W :

5. You are not required to accept this‗
License, since you have not signed it.‗
However, nothing else grants you permission‗
to modify or distribute the Program or its‗
derivative works. These actions are
prohibited by law if you do not accept this‗
License. Therefore, by modifying or‗
distributing the Program (or any work based‗
on the Program), you indicate your‗
acceptance of this License to do so, and‗
all its terms and conditions for copying,‗
distributing or modifying the Program or‗
works based on it.

See you later,

Best Regards,

guy038

That is how genius answers! Wow I’m impressed!! I didn’t realize that my purpose can be achieved!

Still haven’t tested it, but it is enough to know what is the possibilities that your help would gets.

To put an understanding to it, I want to capture the text before, between, and after the string [NEWLINE] and change their order from \1\2\3\4 to \4\3\2\1.
I can achieve this by first replacing the string [NEWLINE] to say ✓✓✓, then capture them by this regex
search: (.+[\x{0000}-\x{9faf}])✓✓✓(.+[\x{0000}\x{9faf}])✓✓✓(.+[\x{0000}\x{9faf}])✓✓✓(.+[\x{0000}\x{9faf}])
Replace : \4[NEWLINE]\3[NEWLINE]\2[NEWLINE]\1

That can be happened, but I know this method can only work up to 9 captured groups, and I had some of them excceding 9 groups.

I was playing around with this idea and I’m not sure I see the importance of introducing the complication of the “longest word in English” stuff. For example, if I experiment with a variant of the regex that ignores this, I still get nice results:

Alan, looking again to my previous post, you’re absolutely right about it. Can’t understand why I thought that the length of words was so important ! I should have been excessively tired, two days ago ;-))

So , I’ve just completely updated my previous post, mentioning your contribution to that nicer regex. Thanks for that !

As for your own S/R, below :

SEARCH (?-s)(.{1,43})\W

REPLACE $1\r\n

It just differs from my last S/R, of my previous post, as it does not take, in account, the final NON-word character, at position 44, in the replacement part !

Therefore, starting, again, from this part of the license.txt file, below :

5. You are not required to accept this License, since you have not signed it. However, nothing else grants you permission to modify or distribute the Program or its derivative works. These actions are prohibited by law if you do not accept this License. Therefore, by modifying or distributing the Program (or any work based on the Program), you indicate your acceptance of this License to do so, and all its terms and conditions for copying, distributing or modifying the Program or works based on it.

It would give the same text, without the space character, at the end of all the lines generated :

5. You are not required to accept this
License, since you have not signed it.
However, nothing else grants you permission
to modify or distribute the Program or its
derivative works. These actions are
prohibited by law if you do not accept this
License. Therefore, by modifying or
distributing the Program (or any work based
on the Program), you indicate your
acceptance of this License to do so, and
all its terms and conditions for copying,
distributing or modifying the Program or
works based on it.

Yes, in playing around with your original regex, I didn’t worry about the resulting space at the end of the line, as I have my “save” shortcut mapped to “trim trailing spaces” + “save”. The ONLY way files should be saved (for me!).

And it is great that you have Admin rights here and can edit old posts, but I’m neutral on this. I think that old posts should not be edited and clarifying posts should just be added on. It is difficult to follow sometimes when history is CHANGED rather than simply CORRECTED/CLARIFIED later. :-D

Yes, you’re right about it : I should have created a new post with the corrections, for a better history ! It’s just that my updated post was, still, quite long and I thought it would be more clear to, simply, change my initial post. But, I do understand your point of view !

To complete the @scott-sumner post, about the two syntaxes of the searched groups, in replacement :

\N, with 0 < N < 10

$N, with 0 <= N < 2,147,483,648

There is the other practical syntax, below :

${N}, with 0 <= N < 2,147,483,648

Indeed, let’s imagine the original text:

abcd
1234
WXYZ

and the first S/R :

SEARCH ^.(..).

REPLACE $100|

You obtain the simple text :

|
|
|

Why ?! Just because, in replacement, the regex engine is looking for the group $100, which, obviously, does not exist ! So, the regex engine rewrites a zero-length string, for the non-existent group 100, followed by the literal character | !

Now, compare, with the second S/R, below :

SEARCH ^.(..).

REPLACE ${1}00|

This time, you, correctly, get the text, below :

bc00|
2300|
XY00|

=> All the changed lines begin by the second and third characters of the original lines of text ( $1 ), and are, simply, followed by the string 00|

I asked before for a way to rearrange the groups between [NEWLINE] to be backward… Now I’m asking for the same but in more automated way…

Because not all lines have the same amount of Groups, I want to arrange all the lines that contains Groups between [NEWLINE] to be backward arrangement.

-Example#3 Contains SIX groups
One [NEWLINE] two [NEWLINE] three [NEWLINE] four [NEWLINE] five [NEWLINE] six
-------
+Seeked arrangement
six[NEWLINE] five [NEWLINE] four [NEWLINE] three [NEWLINE] two [NEWLINE]One

While using the same regex or python script

-Example#4 Contains 4 groups
I want [NEWLINE] this [NEWLINE] to be [NEWLINE] last
------
+Seeked arrangement
last[NEWLINE] to be [NEWLINE] this [NEWLINE]I want

I found a general method, which uses three consecutive S/R. We’ll need two dummy characters, NOT used in the current file. I, personally, chose the # and @ characters, but any other may be used !

The first S/R :

Changes any string [NEWLINE], possibly preceded and/or followed with a space character, by the dummy character #

Adds, also, a # character at the end of any non-blank line

The second S/R is the main S/R, which rewrites the different parts, between the # character, in reverse order.

Note that this S/R will have to be performed as many times, till the message Replace All: 0 occurrences were replaced occurs, in the Replace dialog

The general idea, about this S/R, is to switch the beginning and ending parts of the found text, adding a @ character, at the end of the exchanged parts, in order that the next run of this S/R, will avoid these moved parts of text ! Hence, the decreasing number of occurrences found, till zero :-))

The Third S/R :

Changes the # character, possibly preceded by a @ character, inside text, by the string [NEXLINE], preceded and followed with a space character

Deletes the # character, possibly preceded by a @ character, when located at the end of the lines

All these S/R will use the Regular expression search mode, the Wrap around option and the Replace All button, of the Replace dialog

So, let’s start with the original text, below :

One [NEWLINE] two [NEWLINE] three [NEWLINE] four [NEWLINE] five [NEWLINE] six [NEWLINE] seven [NEWLINE] eight [NEWLINE] nine [NEWLINE] ten [NEWLINE] eleven [NEWLINE] twelve
One [NEWLINE] two [NEWLINE] three [NEWLINE] four [NEWLINE] five [NEWLINE] six [NEWLINE] seven [NEWLINE] eight [NEWLINE] nine [NEWLINE] ten [NEWLINE] eleven
One [NEWLINE] two [NEWLINE] three [NEWLINE] four [NEWLINE] five [NEWLINE] six [NEWLINE] seven [NEWLINE] eight [NEWLINE] nine [NEWLINE] ten
Other text NOT concerned
by this Search Replacement
One [NEWLINE] two [NEWLINE] three [NEWLINE] four [NEWLINE] five [NEWLINE] six [NEWLINE] seven [NEWLINE] eight [NEWLINE] nine
One [NEWLINE] two [NEWLINE] three [NEWLINE] four [NEWLINE] five [NEWLINE] six [NEWLINE] seven [NEWLINE] eight
One [NEWLINE] two [NEWLINE] three [NEWLINE] four [NEWLINE] five [NEWLINE] six [NEWLINE] seven
Bla bla blah
Bla bla blah
Bla bla blah
One [NEWLINE] two [NEWLINE] three [NEWLINE] four [NEWLINE] five [NEWLINE] six
One [NEWLINE] two [NEWLINE] three [NEWLINE] four [NEWLINE] five
One [NEWLINE] two [NEWLINE] three [NEWLINE] four
Dummy text
inserted, in between !
One [NEWLINE] two [NEWLINE] three
One [NEWLINE] two
One
I want [NEWLINE] this [NEWLINE] to be [NEWLINE] last

Seven consecutive runs of that regex S/R are required, to get the sought text :

Run 1 : 12 occurrences replaced

Run 2 : 10 occurrences replaced

Run 3 : 7 occurrences replaced

Run 4 : 5 occurrences replaced

Run 5 : 3 occurrences replaced

Run 6 : 1 occurrences replaced

Run 7 : 0 occurrences replaced

Note : After each run, you may hit the Find Next button, before hitting the Replace All button, to guess the general process !

The part [^@#\r\n], in the searched regex, represents any single character, different from @, #, \n and \r

Then, after running the last S/R, once :

SEARCH (?-s)(@?#)(?=.)|@?#

REPLACE ?1\x20[NEWLINE]\x20

We obtain our final text :

twelve [NEWLINE] eleven [NEWLINE] ten [NEWLINE] nine [NEWLINE] eight [NEWLINE] seven [NEWLINE] six [NEWLINE] five [NEWLINE] four [NEWLINE] three [NEWLINE] two [NEWLINE] One
eleven [NEWLINE] ten [NEWLINE] nine [NEWLINE] eight [NEWLINE] seven [NEWLINE] six [NEWLINE] five [NEWLINE] four [NEWLINE] three [NEWLINE] two [NEWLINE] One
ten [NEWLINE] nine [NEWLINE] eight [NEWLINE] seven [NEWLINE] six [NEWLINE] five [NEWLINE] four [NEWLINE] three [NEWLINE] two [NEWLINE] One
Other text NOT concerned
by this Search Replacement
nine [NEWLINE] eight [NEWLINE] seven [NEWLINE] six [NEWLINE] five [NEWLINE] four [NEWLINE] three [NEWLINE] two [NEWLINE] One
eight [NEWLINE] seven [NEWLINE] six [NEWLINE] five [NEWLINE] four [NEWLINE] three [NEWLINE] two [NEWLINE] One
seven [NEWLINE] six [NEWLINE] five [NEWLINE] four [NEWLINE] three [NEWLINE] two [NEWLINE] One
Bla bla blah
Bla bla blah
Bla bla blah
six [NEWLINE] five [NEWLINE] four [NEWLINE] three [NEWLINE] two [NEWLINE] One
five [NEWLINE] four [NEWLINE] three [NEWLINE] two [NEWLINE] One
four [NEWLINE] three [NEWLINE] two [NEWLINE] One
Dummy text
inserted, in between !
three [NEWLINE] two [NEWLINE] One
two [NEWLINE] One
One
last [NEWLINE] to be [NEWLINE] this [NEWLINE] I want

The search part looks for the regex @?#, either, inside the lines ( case group 1 defined ) or at end of lines ( case NO group 1 )

The replacement part means that, IF group 1 exists, the searched text is replaced by the string [NEWLINE], surrounded by space characters, ELSE NO replacement occurs

I modified the original Search regex, as it catches some Unicode characters with will break the line in a middle of a word. So in the modified regex I replace \W with \x20 (space character)… so far no word breaking issues
Here is the modified one
(?-s).{1,44}(?=\x20)