Community
    • Login

    regex: put comments at the beginning and end of the line (div tags html) before and after some words

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    3 Posts 3 Posters 559 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.
    • Robin CruiseR
      Robin Cruise
      last edited by

      hei people. I have 900 html files, in which I have a line like this, but every files as a different html link

      <div class="fb-comments" data-href="https://my-website.com/article-11.html" data-numposts="5"></div>

      I want to put this line in the comments, as to become:

      <!-- Facebook Comments <div class="fb-comments" data-href="https://my-website.com/article-11.html" data-numposts="5"></div> -->

      The easiest way is a simple search and replace, first part and last part of the line:

      Search: <div class="fb-comments" Replace BY: <!-- Facebook Comments <div class="fb-comments"
      Search: data-numposts="5"></div> Replace BY: data-numposts="5"></div> -->

      but it will be easier to make this with a regex. Can anyone help me?

      PeterJonesP 1 Reply Last reply Reply Quote 0
      • PeterJonesP
        PeterJones @Robin Cruise
        last edited by

        @Robin-Cruise,

        The easiest way I can think of is to match for arbitrary text between the fb-comments and the data-numposts. I am going to assume that the <div ...></div> is all on one line (since you didn’t show linebreaks) and I am going to anchor to the beginning of the line, so that a line that was already transformed won’t accidentally be transformed again.

        • FIND = (?-s)^<div class="fb-comments".*?data-numposts="5"></div>
          • first parens turn off .-matches-newline
          • the caret ^ anchors to the beginning of the line (no whitespace or other characters before the <div
          • the .*? is a non-greedy to allow anything between the fb-comments and data-numposts
        • REPLACE = <!-- Facebook Comments $0 -->
          • the $0 is the value of the entire match from FIND, so everything in the <div...></div>
        • MODE = regular expression

        Given the assumptions I explained and the example data you showed, this worked for me.

        Remember, the npp-user-manual.org site has a rather large and detailed page on searching, with a lot of information on regular expressions. As someone who has had multiple search-and-replace questions recently, you seem to be one who could benefit from studying that page. I know it can be overwhelming amount of information, but it will help you in the long run much more than just asking us to develop the regex for you.

        ----

        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 literal text using the </> toolbar button or manual Markdown syntax. To make regex in red (and so they keep their special characters like *), use backticks, like `^.*?blah.*?\z`. Screenshots can be pasted from the clipboard 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 2
        • guy038G
          guy038
          last edited by guy038

          Hello @robin-cruise and All,

          Why not this S/R :

          SEARCH (?-s)^\h*<div class="fb-comments" data-href=".+

          REPLACE <!-- Facebook Comments $0 -->

          Notes :

          This regex would search all contents of any line, beginning with the string <div class="fb-comments" data-href=, possibly predeced with any leading horizontal blank chars, and places this line in comments, with the syntax <!-- Facebook Comments ............... -->

          Not a very difficult regex : you could have found out this one, by yourself ;-))

          Best Regards,

          guy038

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