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 2.1k Views 1 Watching
    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.
    • Juuso UuJ Offline
      Juuso Uu
      last edited by

      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?

      astrosofistaA 1 Reply Last reply Reply Quote 0
      • astrosofistaA Offline
        astrosofista @Juuso Uu
        last edited by

        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
        • Juuso UuJ Offline
          Juuso Uu
          last edited by

          Worked perfectly, thank you so much!!

          1 Reply Last reply Reply Quote 1
          • Anandkumar PrajapatiA Offline
            Anandkumar Prajapati
            last edited by

            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

            Hello! It looks like you're interested in this conversation, but you don't have an account yet.

            Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

            With your input, this post could be even better 💗

            Register Login
            • First post
              Last post
            The Community of users of the Notepad++ text editor.
            Powered by NodeBB | Contributors