Community
    • Login

    Replacing every line that ends in X with Y

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    regex
    5 Posts 2 Posters 2.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.
    • Aulumen EulogyA
      Aulumen Eulogy
      last edited by

      I need to replace every line that ends in a certain string of characters (.png in this case) with a different line - I’ve tried to figure out how it can be done with regex however I can’t seem to get my head around how regex works. I can also post a snippet of the file (although it’s nearing 400,000 lines which is why I can’t do it manually). Any help with this is greatly appreciated

      1 Reply Last reply Reply Quote 0
      • guy038G
        guy038
        last edited by guy038

        Hello, @aulumen-eulogy,

        Numerous things can be achieved with regular expressions, indeed ! Of course, we need some additional information to build an accurate search/replacement which could meet your needs ;-))

        Just a guess : I suppose that your file contains lines which end with a picture file name, having .png format and that you want to change these names with, …let’s say, the .jpg extension ! If so :

        • Open the Replace dialog ( Ctrl+ H )

        • SEARCH \.png$

        • REPLACE .jpg

        • Click, preferably, the Wrap around option

        • Select the Regular expression search mode

        • Click on the Replace All button

        Notes :

        • The dot character must be escaped with the backslash char \ , as it’s a meta-character, with special signification, in regexes

        • At the end, the $ symbol is an assertion, which means that the literal string .png must be located at the end of each line, in order to get a match

        Best Regards,

        guy038

        Aulumen EulogyA 2 Replies Last reply Reply Quote 3
        • Aulumen EulogyA
          Aulumen Eulogy @guy038
          last edited by

          @guy038

          Thank you for the in-depth answer, although it’s not precisely what I’m looking for - Say I have 4 lines:
          imageone.png
          imagetwo.jpg
          unrelatedline
          imagethree.png
          Instead of replacing the file extension with something else, I need to replace any lines ending in .png with a different line (such as imagereplaced.png), changing the previous lines like so:
          imagereplaced.png
          imagetwo.jpg
          unrelatedline
          imagereplaced.png

          I’m sorry if I wasn’t clear with this in my original post

          1 Reply Last reply Reply Quote 0
          • guy038G
            guy038
            last edited by guy038

            Hi, @aulumen-eulogy and All,

            Sorry, but there’s still a problem ! In your list of replaced lines, below :

            imagereplaced.png
            imagetwo.jpg
            unrelatedline
            imagereplaced.png
            

            There are two identical lines imagereplaced.png ?!

            Thus, two possibilities :

            A) The part “replaced” is really a literal string and you do not mind having duplicates names of file. In that case, the following regex S/R should work :

            • SEARCH (?i)\bimage.+\.png$

            • REPLACE imagereplaced.png

            Notes :

            • The (?i) part is an in-line modifier meaning that the search is insensitive to the case of letters

            • The \b part is as assertion, saying that the string image must be preceded with a non-word character ( EOL, blank or symbol character )

            • The .+ represents the longest area of characters, between the two strings image and .png, which must end each line, due to the final $ meta-character


            B) The part, naned “replaced”, is different for each file. In that case, again… some explanations would be welcome :-))

            Cheers,

            guy038

            1 Reply Last reply Reply Quote 3
            • Aulumen EulogyA
              Aulumen Eulogy @guy038
              last edited by

              @guy038
              Many thanks for the help, A worked wonderfully (I replaced image with [a-z] so it’d work with all of the different files I needed to replace, but aside from that it was flawless).

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