Community
    • Login

    How to insert text at the end of every url?

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    3 Posts 2 Posters 926 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.
    • Rob KR
      Rob K
      last edited by

      I have a big text file that contains urls that all begin with the same domain name but vary in the rest of the url, is there any way to search for the line beginning with http://test.domain and insert text at the end of the url ? i just need to add some text to the end of each url.

      the urls are contained on seperate lines and are not clashing with anything so a simple method of finding the http://test.domain at the start of the line and inserting text at the end of the line will work .

      PeterJonesP 2 Replies Last reply Reply Quote 0
      • PeterJonesP
        PeterJones @Rob K
        last edited by

        This makes the assumption that there will never be a space inside your URL. For robustness, I allowed spaces to optionally occur before the URL, but I assumed nothing other than space came before the URL. If there is something after the URL, it must have a space before it.

        if I have data like the following:

        http://test.domain/a/b/c?d%3Defg blah
            http://test.domain:8080/4/3.html blah
            http://test.domain/a/b/c?d%3Defg
        http://test.domain:8080/4/3.html
        https://test.domain/a/b/c?d%3Defg blah
            https://test.domain:8080/4/3.html blah
            https://test.domain/a/b/c?d%3Defg
        https://test.domain:8080/4/3.html
        

        and run

        • FIND = (?i-s)^\h*https?://test.domain\S*
        • REPLACE = $0 new stuff
        • MODE = regular expression

        it will become

        http://test.domain/a/b/c?d%3Defg new stuff blah
            http://test.domain:8080/4/3.html new stuff blah
            http://test.domain/a/b/c?d%3Defg new stuff
        http://test.domain:8080/4/3.html new stuff
        https://test.domain/a/b/c?d%3Defg new stuff blah
            https://test.domain:8080/4/3.html new stuff blah
            https://test.domain/a/b/c?d%3Defg new stuff
        https://test.domain:8080/4/3.html new stuff
        

        I think that’s close to what you want. If it’s not what you want, you’ll have to follow my advice below to make things more clear as to what you want.

        ----

        Do you want regex search/replace help? Then please be patient and polite, show some effort, and be willing to learn; answer questions and requests for clarification that are made of you. All example text should be marked as plain text using the </> toolbar button or manual Markdown syntax. Screenshots can be pasted from the clipbpard to your post using Ctrl+V to show graphical items, but any text should be included as literal text in your post so we can easily copy/paste your data. Show the data you have and the text you want to get from that data; include examples of things that should match and be transformed, and things that don’t match and should be left alone; show edge cases and make sure you examples are as varied as your real data. Show the regex you already tried, and why you thought it should work; tell us what’s wrong with what you do get… Read the official NPP Searching / Regex docs and the forum’s Regular Expression FAQ. If you follow these guidelines, you’re much more likely to get helpful replies that solve your problem in the shortest number of tries.

        1 Reply Last reply Reply Quote 1
        • PeterJonesP
          PeterJones @Rob K
          last edited by

          @Rob-K ,

          I just said:

          • FIND = (?i-s)^\h*https?://test.domain\S*
          • REPLACE = $0 new stuff

          The FIND would be better as:

          • FIND = (?i-s)^\h*https?://test\.domain\S*

          Quick explanation of the regex:

          • the stuff in parentheses set up options to make it case insensitive and to not let . match newline

          • ^ means the match starts at the beginning of the line

          • \h* allows 0 or more horizontal spaces (tabs and space characters) before the url

          • https?:// allows either http or https

          • \. escapes the ., so it means a literal domain separator, not the regex wildcard character

          • \S* means that there can be 0 or more additional non-space characters after the domain; since the end-of-line sequence counts as space character, it will stop at the end of the line if there’s nothing after the URL

          • $0 in the REPLACE means the entire matched text – so everything from beginning of line to just before the first space (or end-of-line).

          • new stuff indicates the new text you want to add

          • This means that the replacement will still include the original URL, plus the new stuff.

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