Search code examples
notepad++

After re-order line, insert a blank line, change order first and second hyphen


My Previous question post in
How can I get a line break After each name change
A good solution that works well has been proposed by Toto Thank You
but, sometimes I can change insert one line after the second hyphen "-"
When I change the order by interverting the order between the first and 2nd hyphen
I want to get this:

before:
Bob Cleatry - August - 6325.32 - Portland, OR, United States
john Cleatry - October - 7289.25 - Portland, OR, United States
Alan Cleatry - Novenber - 12.582.00 - Portland, OR, United States
Peter J. Finley - September - 5312.00 - New Jersey, United States
Peter J. Finley - October - 7325.00 - New Jersey, United States
Peter J. Finley - Novenber - 8240.00 - New Jersey, United States
jack Norton - October - 12425.60 - New York, United States
celine Norton - November - 13328.32 - New York, United States

Re-order With Regex:
First Step 1
Find: ^(.+?)\s-\s+(.+?)\s-\s+(.+?)\s-\s
Replace: $2 - $1 - $3

Nota: I Append space before and after hyphen "-" for éliminate name Compound, Composite ex: jean-claude and hyphen in URL etc...

After Step 1.
August - Bob Cleatry - 6325.32Portland, OR, United States
October - john Cleatry - 7289.25Portland, OR, United States
Novenber - Alan Cleatry - 12.582.00Portland, OR, United States
September - Peter J. Finley - 5312.00New Jersey, United States
October - Peter J. Finley - 7325.00New Jersey, United States
Novenber - Peter J. Finley - 8240.00New Jersey, United States
October - jack Norton - 12425.60New York, United States
November - celine Norton - 13328.32New York, United States

I want to get this:

August - Bob Cleatry - 6325.32Portland, OR, United States
October - john Cleatry - 7289.25Portland, OR, United States
Novenber - Alan Cleatry - 12.582.00Portland, OR, United States

September - Peter J. Finley - 5312.00New Jersey, United States
October - Peter J. Finley - 7325.00New Jersey, United States
Novenber - Peter J. Finley - 8240.00New Jersey, United States

October - jack Norton - 12425.60New York, United States
November - celine Norton - 13328.32New York, United States


Solution

  • You might add a step before the re-ordering, matching all lines that start with for example:

    ^(.+?)\s-\s.*(?:\R\1.*)+
    

    Regex demo

    Then in the replacement, use the full match surrounded by a newline.

    \n$0\n
    

    For the second step, you can use the approach that you already use in the question.


    Assuming there are no newlines already, and if you don't want to add extra newlines at the start or at the end of the string when replacing, you can use a conditional replacement.

    Find what:

    (?:(^)|\R)((.+?)\s-\s.*(?:\R\3.*)+)(?:(\R)|$)
    

    Replace with:

    (?{1}:\n\n)$2(?{4}\n\n:)
    

    enter image description here

    For the re-ordering

    enter image description here