Community

    • Login
    • Search
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search

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

    Help wanted · · · – – – · · ·
    3
    4
    304
    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 Uu
      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?

      astrosofista 1 Reply Last reply Reply Quote 0
      • astrosofista
        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 Uu
          Juuso Uu last edited by

          Worked perfectly, thank you so much!!

          1 Reply Last reply Reply Quote 1
          • Anandkumar Prajapati
            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
            • First post
              Last post
            Copyright © 2014 NodeBB Forums | Contributors