Excluding all of a Line when using Add / Replace



  • Hello everyone. I am very new to NotePad++ and various strategies that can be used. Please bear with me.

    I’d like to use the find/replace without affecting or ALWAYS EXCLUDING one line in particular, “Original Location”

    Here is an example:


    Original Name: A Ha Take On Me
    Proposed Name: A HA - TAKE ON ME
    File Extension: .mp3
    Total Tag Count: 4
    Duplicate Tags Found: YES
    Original Location: J:\Seagate - ZIP\SC - Sound Choice\SC 01-18\A Ha - Take On Me\A Ha Take On Me.mp3

    Let’s say I want to find every occurrence of "A HA - " and replace it with
    "AHA - "
    This would be problematic as I can’t touch the “Original Location”. This is the FILE NAME and PATH. I have a script that needs to see it exactly as it is.

    Any ideas how I can tell NotePad++ to place this “Original Location” line in a “read only” mode (if you will), or EXCLUDE it from modifications altogether? There will be thousands of my above example stacked in a text file like this:

    Original Name: 10000 Maniacs Because The Night
    Proposed Name: 10000 MANIACS - BECAUSE THE NIGHT
    File Extension: .cdg
    Total Tag Count: 2
    Duplicate Tags Found: NO
    Original Location: J:\Seagate - ZIP\SC - Sound Choice\SC 01-18\10000 Maniacs - Because The Night\10000 Maniacs Because The Night.cdg

    Original Name: 10000 Maniacs Because The Night
    Proposed Name: 10000 MANIACS - BECAUSE THE NIGHT
    File Extension: .mp3
    Total Tag Count: 2
    Duplicate Tags Found: NO
    Original Location: J:\Seagate - ZIP\SC - Sound Choice\SC 01-18\10000 Maniacs - Because The Night\10000 Maniacs Because The Night.mp3

    Original Name: Im Not In Love
    Proposed Name: IM NOT IN LOVE
    File Extension: .cdg
    Total Tag Count: 1
    Duplicate Tags Found: YES
    Original Location: J:\Seagate - ZIP\SC - Sound Choice\SC 01-18\10CC - Im Not In Love\ Im Not In Love.cdg

    Original Name: Im Not In Love
    Proposed Name: IM NOT IN LOVE
    File Extension: .mp3
    Total Tag Count: 1
    Duplicate Tags Found: YES
    Original Location: J:\Seagate - ZIP\SC - Sound Choice\SC 01-18\10CC - Im Not In Love\ Im Not In Love.mp3

    Any suggestions? Thank you all, in advance, for your time.



  • @Steven-Warrens ,

    It is possible to do using regular expression mode, as long as the Original Location and following can be assumed to always be on the same line (no newlines in between).

    But do you really want to replace that text anywhere other than Original Location lines, or do you really just want to do it in Proposed Name:? If anywhere, do you want case-sensitive (so it would only match A HA and not A Ha, or do you want it to match both.

    If you wanted it just on Proposed Name: line, it would be really easy

    • FIND = (?-s)(^Proposed Name:.*)A HA(.*)$
    • REPLACE = ${1}AHA${2}
    • Search Mode = Regular Expression

    But if your data file has thousands of multi-line entries, and the ones you show are representative, then I’m guessing you really have a long list of mappings that you want to do, like:

    • A HA => AHA
    • IM_ => I'm_ (I used underscore _ instead of a space so it’s visible)
    • 10000_ => 10,000_

    or some such set of mappings.

    In which case, if you have more than a few, I’d be tempted to use a scripting plugin and write a script which will go through each of those, and apply the same general procedure to each.

    Let us know more of what you want.



  • Why not something like this:

    find: (?i-s)^(?!Original Location:)(.*?)a ha
    repl: ${1}aha
    search mode: regular expression

    Of course, some minor issues, like “casing” and multiple occurrences of a ha on a line (would have to run a Replace All multiple times to clear that up) could remain…



  • @Alan-Kilborn said in Excluding all of a Line when using Add / Replace:

    Why not

    Because I was thinking “you cannot have arbitrary length negative lookbehind”, and forgot that I could pull the .* out of the lookbehind. :-)

    Other than that, your solution is much simpler than mine.

    Though I still think if he’s really got a whole list of substitutions that a scripting solution will serve him better.



  • @Steven-Warrens, @Alan-Kilborn, @PeterJones

    You were too fast for me!

    Anyway, here is my take:

    Search: (?-s)Original Location:.*\K|(A Ha)
    Replace: ?1AHA
    

    Take care and have fun!



  • BTW, @Steven-Warrens ,

    Your example data has lots of <H2> in its HTML rendering. I am thinking your sample data may have started with one or more # characters. If so, the forum sees that as Markdown.

    Please format sample data using the </> button on the toolbar when entering your post:

    In fact, read my common advice below… it should help you get better answers, faster.

    ----

    Do you want regex search/replace help? Then please be patient and polite, show some effort, and be willing to learn; answer questions and requests for clarification that are made of you. All example text should be marked as literal text using the </> toolbar button or manual Markdown syntax. To make regex in red (and so they keep their special characters like *), use backticks, like `^.*?blah.*?\z`. Screenshots can be pasted from the clipboard to your post using Ctrl+V to show graphical items, but any text should be included as literal text in your post so we can easily copy/paste your data. Show the data you have and the text you want to get from that data; include examples of things that should match and be transformed, and things that don’t match and should be left alone; show edge cases and make sure you examples are as varied as your real data. Show the regex you already tried, and why you thought it should work; tell us what’s wrong with what you do get. Read the official NPP Searching / Regex docs and the forum’s Regular Expression FAQ. If you follow these guidelines, you’re much more likely to get helpful replies that solve your problem in the shortest number of tries.



  • Hello, @steven-warrens, @alan-kilborn, @Astrosofista, @peterjones, @terry-r and All,

    At least…, I’ve solved this general task, which is :

    To Replace and/or delete specific expression(s), ONLY  on lines which contains or not contain, an other specific string !


    The generic regex S/R are :

    • If replacement must occur on the lines which contains the CONDITION_String :

      • SEARCH (?-is)(?:^(?=.*CONDITION_String)|\G).*?\K(?:(SEARCHED_String_1)|(SEARCHED_String_2)|......|(SEARCHED_String_N)|String1_TO_DELETE|String_2_TO_DELETE|...)

      • REPLACE (?1REPLACED_String_1)(?2REPLACED_String_2)...........(?NREPLACED_String_N)

    • If replacement must occur on the lines which do NOT contain the CONDITION_String :

      • SEARCH (?-is)(?:^(?!.*CONDITION_String)|\G).*?\K(?:(SEARCHED_String_1)|(SEARCHED_String_2)|......|(SEARCHED_String_N)|String1_TO_DELETE|String_2_TO_DELETE|...)

      • REPLACE (?1REPLACED_String_1)(?2REPLACED_String_2)...........(?NREPLACED_String_N)

    Note that these two regex S/R are almost identical :

    • The first one use a positive look-ahead (?=....) structure

    • The second one use a negative look-ahead (?!....) structure


    For instance, given this sample :

    ABCDEF    WORD_1    WORD_2    WORD_3    WORD_4    WORD_5
    XXXXXX    WORD_1    WORD_2    WORD_3    WORD_4    WORD_5
    
    WORD_1    WORD_2    WORD_3    ABCDEF    WORD_4    WORD_5
    WORD_1    WORD_2    WORD_3    XXXXXX    WORD_4    WORD_5
    
    WORD_1    WORD_2    WORD_3    WORD_4    WORD_5    ABCDEF
    WORD_1    WORD_2    WORD_3    WORD_4    WORD_5    XXXXXX
    

    This test text contains :

    • Three lines with text ABCDE, at beginning, middle and end of lines, which I suppose to be the condition

    • Three lines with text XXXXX, so not ABCDEF, at beginning, middle and end of lines

    Then the following regex S/R :

    SEARCH (?-is)(?:^(?=.*ABCDE)|\G).*?\K(?:(WORD_1)|(WORD_3)|(WORD_2|WORD_4)|\x20*WORD_5)

    REPLACE (?1ABCD_1)(?2ABCD_2)(?3------)

    gives :

    ABCDEF    ABCD_1    ------    ABCD_2    ------
    XXXXXX    WORD_1    WORD_2    WORD_3    WORD_4    WORD_5
    
    ABCD_1    ------    ABCD_2    ABCDEF    ------
    WORD_1    WORD_2    WORD_3    XXXXXX    WORD_4    WORD_5
    
    ABCD_1    ------    ABCD_2    ------    ABCDEF
    WORD_1    WORD_2    WORD_3    WORD_4    WORD_5    XXXXXX
    

    As you can see, the replacements occurred on lines which contain the string ABCDEF

    And the following regex S/R :

    REPLACE (?-is)(?:^(?!.*ABCDE)|\G).*?\K(?:(WORD_1)|(WORD_3)|(WORD_2|WORD_4)|\x20*WORD_5)

    REPLACE (?1ABCD_1)(?2ABCD_2)(?3------)

    Outputs :

    ABCDEF    WORD_1    WORD_2    WORD_3    WORD_4    WORD_5
    XXXXXX    ABCD_1    ------    ABCD_2    ------
    
    WORD_1    WORD_2    WORD_3    ABCDEF    WORD_4    WORD_5
    ABCD_1    ------    ABCD_2    XXXXXX    ------
    
    WORD_1    WORD_2    WORD_3    WORD_4    WORD_5    ABCDEF
    ABCD_1    ------    ABCD_2    ------    XXXXXX
    

    This time, the replacements occurred on lines which do not contain the string ABCDEF


    Now, @steven-warrens, it’s quite easy to solve your problem, from above ;-))

    • Your CONDITION_String is the string Original Location:, with this exact case

    • The SEARCHED strings are the strings A HA - and A Ha -

    • The REPLACED strings are the strings AHA - and AHa -

    …leading to this simple regex S/R :

    REPLACE (?-is)(?:^(?!.*Original Location:)|\G).*?\K(?:(A HA -)|(A Ha -))

    REPLACE (?1AHA -)(?2AHa -)

    So, assuming that initial text :

    Original Name: A Ha Take On Me
    Proposed Name: A HA - TAKE ON ME
    File Extension: .mp3
    Total Tag Count: 4
    Duplicate Tags Found: YES
    Original Location: J:\Seagate - ZIP\SC - Sound Choice\SC 01-18\A Ha - Take On Me\A Ha Take On Me.mp3
    
    Original Name: A Ha Take On Me
    Proposed Name: A HA - TAKE ON ME
    File Extension: .mp3
    Total Tag Count: 4
    Duplicate Tags Found: YES
    Original Location: J:\Seagate - ZIP\SC - Sound Choice\SC 01-18\A Ha - Take On Me\A Ha Take On Me.mp3
    

    you’ll get :

    Original Name: A Ha Take On Me
    Proposed Name: AHA - TAKE ON ME
    File Extension: .mp3
    Total Tag Count: 4
    Duplicate Tags Found: YES
    Original Location: J:\Seagate - ZIP\SC - Sound Choice\SC 01-18\A Ha - Take On Me\A Ha Take On Me.mp3
    
    Original Name: A Ha Take On Me
    Proposed Name: AHA - TAKE ON ME
    File Extension: .mp3
    Total Tag Count: 4
    Duplicate Tags Found: YES
    Original Location: J:\Seagate - ZIP\SC - Sound Choice\SC 01-18\A Ha - Take On Me\A Ha Take On Me.mp3
    

    The replacements occur ONLY  on the two lines beginning with Proposed Name:. This is logical because :

    • The part A Ha, in the first line, does not end with a dash - so it is not concerned by the replacement

    • the searched string A Ha -, in the last line, is not replaced too, because the current line contains the string Original Location:, with this exact case

    I hope, you’ve understood the general technique. If any problem just ask us !

    Best Regards,

    guy038



  • A BIG thanks to all for your help. It looks like I will be using several different combinations of suggestions offered here. Wish me luck and thanks again!!


Log in to reply