Regex: Rename Duplicate lines with <!— comments -->



  • hello. I faced a problem, I found the solution, maybe someone needs it.

    If I have more lines with the same content, such as, and I want to rename each one, starting with the last instances:

    <!— comments -->
    ...
    <!— comments -->
    
    bla blah
    
    <!— comments -->
    ...
    <!— comments -->
    

    My solution is this:

    FIND: (?-s)(?:.*\R)*?\K.*(<\!— comments -->).*(?s)(\R.*)\1

    REPLACE BY: \1\2 <!— comments_NEW -->

    after find and replace, (twice, three times, 4 time…) will become:

    <!— comments -->
    ...
    <!— comments -->
    
    <!— comments_NEW -->
    ...
    <!— comments_NEW -->
    

    so on…



  • @Robin-Cruise

    In general I’m not sure how useful this is, because the stated goal was to eliminate duplicated lines by renaming them, you’ve still got duplicated lines in the result you show, but now you have TWO sets of duplicates instead of just one. If this is useful for you, then Great!, but I don’t see the utility of this as a general technique where “someone needs it”.

    But I wouldn’t have written to say only that.

    I also wanted to point out that your regular expression is in danger of overflowing the engine if the duplicated lines are too far apart in a large file.



  • In my case, I wanted to exchange something between the comments, and I have more comments. So, to do that I had to rename the comments so as not to change everything, and only certain comments.


Log in to reply