Formula in Replace function

  • Hi

    I’ve a long file with names and the following format:
    {“79962”:“D001 | Królewska Demagogia”,“80715”:“D001 | Królewska Demagogia”,“80853”:“D001 | Królewska Demagogia”,“80888”:“D001 | Królewska Demagogia”,“81392”:“D001 | Królewska Demagogia”,“81691”:“D001 | Królewska Demagogia”,“82517”:“D001 | Królewska Demagogia”,“82725”:“D001 | Królewska Demagogia”,“82800”:“D001 | Królewska Demagogia”,“83054”:“D001 | Królewska Demagogia”,“83391”:“D001 | Królewska Demagogia”,“83434”:“D001 | Królewska Demagogia”,“83834”:“D001 | Królewska Demagogia”,“83888”:“D001 | Królewska Demagogia”,“83946”:“D001 | Królewska Demagogia”,“83979”:“D001 | Królewska Demagogia”,“84041”:“D001 | Królewska Demagogia”,“84042”:“D001 | Królewska Demagogia”,“84120”:“D001 | Królewska Demagogia”,“84127”:“D001 | Królewska Demagogia”,“84142”:“D001 | Królewska Demagogia”}

    That’s just a small fragment of the full list of about 350+ names.
    Therefore I’m looking for some automated way…

    I would like to replace the “D001” part of the name with D002, D003, D004 etc to have unique names.
    So it’s n=n+1 starting with n = 001. (Must be 3-digit number).

    The 5-digit numbers in the file are ID’s and should remain the same.

  • @wielki-asasyn

    If I wouldn’t have “dropped the ball” on the script I promised HERE…it’s still in the set of open tabs in my N++…I could point to that as a solution. :-(

  • @wielki-asasyn said in Formula in Replace function:

    I would like to replace the “D001” part of the name with D002, D003, D004 etc to have unique names.

    I thought I knew what you wanted, proceeded to produce a solution using regular expressions, then once I read your post again I’m not sure I have the correct interpretation. So I’ll ask some questions.

    1. The single line of example above, are there about 350+ similar lines of text needing replacing?
    2. Is the single line provided actually a portion of the 1 (and only) line in the file thus within that 1 line there will be approximately 350+ names?

    If the answer to #2 is yes, that’s the idea I followed. My solution involves a few steps. Firstly we’d need to cut this down into individual lines so each name was on a line by itself. Then we’d use the column editor to add an increasing number to the start of the line. Then we’d replace the existing “Dnnn” string with the number at the start of the line. Lastly we’d merge the records back into the original format.

    So my steps are.
    Using the Replace Function we have
    Find What:\|[^”]+”(?!})\K
    Replace With:\r\n

    Search mode MUST be regular expression. Click on the Replace All button.
    So now each name should be on a separate line. Note the regex uses a negative lookahead so that we don’t create a line with the } on it by itself.

    Now the cursor must be placed at the first line, first position, actually immediately before the first character. We use the Column Editor (under Edit main menu) and select “number to insert” and use initial number of 1, increase by 1 and leading zeros MUST be ticked, click OK. Provided you have correctly located the cursor you will now see every line is preceded by a 3 digit number, increasing by 1 each line. Since you say there were approximately 350+ names every number created should be 3 digits long as you requested.

    Now we use another regex to move this, replacing the existing number using Replace function.
    Find What:^(\d{3})(.+?)(“D)\d{3}
    Replace With:\2\3\1

    Again search mode MUST be regular expression and wrap around ticked. Click on the Replace All button

    Confirm the numbers have been correctly replaced. Last step is to join the separated lines back to the original format. So again using the Replace function.
    Find What:[^}]\K\R
    Replace With: nothing in this field, leave it empty.

    Click on the Replace All button and now you should have the format you wished with unique numbers for each name.


Log in to reply