Search code examples
yamlnotepad++config

I'm trying to mass replace some #'s in the middle of a line of text


Caveat: I'm not a coder, but I'm trying to replace 1000+ numbers in a file (I will have to adapt this to fit several files to make the alterations I want, for example: in another file, I might have to change "Volume: 1000 #some comment") and don't want to do it by hand. I've been trying to figure this out for about 4 hours now, and can't make the connection to finish the problem. I'm pretty good at reverse engineering things, but I haven't been able to find an example that I can adapt to my needs.

Editing being done in Notepad++

I have gotten something to work but only if the # I'm trying to replace is at the END of the line.

Example of what I'm trying to Replace: (there are no Tab's, only spaces)

Mass: 500    #this is a commented-out description of this line
#Mass: 5000    #Using a "#" at the beginning of a line comments the entire line.
Mass: 50000    #Not all lines will have a comment
Mass: 900
Mass: 7860    #The Line above has no comment
Mass: 66    #The comments can change but are always at the end
Mass: 6909    #All comments are preceded by a "#" in the game config files.

Expected Results with working regex

Mass: 50    #this is a commented-out description of this line
#Mass: 500    #Not all lines will have a comment
Mass: 5000    #Like the one below this line
Mass: 90
Mass: 786    #The Line above has no comment
Mass: 6    #this is a commented-out description of this line
Mass: 690    #this is a commented-out description of this line

NOTE: All I'm trying to do is remove the LAST digit of any number preceded by the text "Mass: "

What I have tried that works, but only on lines like below:

(^[Mass: ]?=|[0-9]$)

Input

Mass: 500
Mass: 5000
Mass: 7860
Mass: 66
Mass: 6909

Expected Result

Mass: 50
Mass: 500
Mass: 786
Mass: 6
Mass: 690

As soon as I run into lines that have something AFTER the number, it fails completely. Hopefully, I've given enough information, I already know I'm gonna feel really dumb that I couldn't find some simple 'key' that fixes the problem, but I'm getting frustrated now and figured I'd just ask for help. Thanks

P.S. I've been using regex101.com to test it, the files are game files with extensions: .yaml and .ecf

They are all text files.

EDIT/ADDENDUM: Thank you so much, I was getting so frustrated before going to bed last night. That being said I made a mistake in my question. Your given answer was perfect for the question "as asked" but my mistake was that the file data is indented like so:

  Mass: 6909    #All comments are preceded by a "#" in the game config files.
  Volume: 500    #this is a commented-out description of this line

Would I be correct in assuming this would be the correct way to fix it?

^\h*#?Mass:\h*\d+\K\d(?=\h)

Mistake/Addendum 2

I'm literally going to copy out a snippet from the file, as it appears that not all the lines that don't have comments end with just the number I want to alter.

In case you're curious, I'm tweaking Impyrion Galactic Survival Files.

  Mass: 1386, type: float, display: true, formatter: Kilogram    # should be dynamic
  Volume: 99, type: float, display: true, formatter: Liter

That is a literal snippet from the file, again all I'm trying to do is trim 1 number off the end of the number directly after the target (in this case "Mass: ", and leave everything else along


Solution

    • Ctrl+H
    • Find what: #?Mass:\h*\d+\K\d(?!\d)
    • Replace with: LEAVE EMPTY
    • TICK Match case
    • TICK Wrap around
    • SELECT Regular expression
    • UNTICK . matches newline
    • Replace all

    Explanation:

    #?          # optional #
    Mass:       # literally
    \h*         # 0 or more horizontal spaces
    \d+         # 1 or more digits
    \K          # forget all we have seen until this position
    \d          # 1 digit
    (?!\d)      # negative lookahead, make sure we haven't a digit after
    

    Screenshot (before):

    enter image description here

    Screenshot (after):

    enter image description here