• Login
Community
  • Login

How do I replace strings of text while retaining numbers in middle of the line?

Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
4 Posts 3 Posters 1.5k Views
Loading More Posts
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J
    Juuso Uu
    last edited by Mar 5, 2021, 10:56 AM

    So, I am modding a game and have a .txt file in which I need to replace multiple, possibly hundreds of lines of this type :

    owns = 116

    to this

    116 = { country_or_non_sovereign_subject_holds = ROOT }

    The numbers after ‘owns =’ can be anything between 1-5000, so it can have 1 to 4 digits

    What would be the easiest way to replace all of the lines in this file?

    A 1 Reply Last reply Mar 5, 2021, 11:43 AM Reply Quote 0
    • A
      astrosofista @Juuso Uu
      last edited by Mar 5, 2021, 11:43 AM

      Hi @Juuso-Uu

      A regular expression would be fine here. I assume the matching string is placed at the begining of a line. If this is not the case, please remove the ^ symbol from the Search expression.

      Search: ^owns = (\d{1,4})
      Replace: $1 = { country_or_non_sovereign_subject_holds = ROOT }
      

      Put the caret at the very beginning of the document, select just the Regular Expression mode and click on Replace All.

      Take care and have fun!

      1 Reply Last reply Reply Quote 5
      • J
        Juuso Uu
        last edited by Mar 5, 2021, 12:35 PM

        Worked perfectly, thank you so much!!

        1 Reply Last reply Reply Quote 1
        • A
          Anandkumar Prajapati
          last edited by Mar 12, 2021, 8:43 AM

          Hello,
          You can replace strings of text while retaining numbers in the middle of the line using RegEx.
          Follow the steps:
          Open your document in N++.
          Go to the start of the document by clicking on ctrl+home.
          Open The Replace Dialogue Box (ctrl+h).
          Under search, mode Select regular expression.
          write the following in the fields.
          Find what: (?i)(?s)owns = (\d{1,4})
          Replace with: $1 = { country_or_non_sovereign_subject_holds = ROOT }
          Click on Replace All.

          Description:
          (?s) enables dot matching newlines.
          (?i) makes match case insensitive.
          (owns) represents a literal string.
          (\d) (digit) matches any single digit (same as [0-9] ).
          {1,4} matches with a 1 to 4 digit number.
          $1 refers to the first match (in this case $1 will refer to the number i.e. 116).

          1 Reply Last reply Reply Quote 0
          4 out of 4
          • First post
            4/4
            Last post
          The Community of users of the Notepad++ text editor.
          Powered by NodeBB | Contributors