Community
    • Login

    How to combine a multi-line html element into one line?

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    3 Posts 3 Posters 169 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.
    • pbarneyP
      pbarney
      last edited by

      I don’t know why I’m having trouble with this, but suppose I have the following:

      <p>Lorem ipsum dolor sit amet, libero turpis non cras ligula, 
      id commodo, aenean est in volutpat amet sodales, 
      porttitor bibendum facilisi </p>
      <p>Donec vivamus. Vel donec et scelerisque vestibulum. 
      Condimentum aliquam, mollit magna velit nec, tempor cursus 
      vitae sit aliquet neque purus.</p>
      <p>Iaculis et dui ullamcorper, non egestas condimentum dui 
      phasellus. Sit non mattis a, leo in imperdiet erat nec 
      pulvinar. Ornare massa justo</p>
      <p>Rhoncus lacinia. Imperdiet nulla sem fringilla, purus 
      enim amet, nascetur faucibus, adipiscing neque ut bibendum, 
      at felis nec in. Mauris</p>
      

      …and I want to put the contents of each <p> element onto the same line:

      <p>Lorem ipsum dolor sit amet, libero turpis non cras ligula, id commodo, aenean est in volutpat amet sodales, porttitor bibendum facilisi </p>
      <p>Donec vivamus. Vel donec et scelerisque vestibulum. Condimentum aliquam, mollit magna velit nec, tempor cursus vitae sit aliquet neque purus.</p>
      <p>Iaculis et dui ullamcorper, non egestas condimentum dui phasellus. Sit non mattis a, leo in imperdiet erat nec pulvinar. Ornare massa justo</p>
      <p>Rhoncus lacinia. Imperdiet nulla sem fringilla, purus enim amet, nascetur faucibus, adipiscing neque ut bibendum, at felis nec in. Mauris</p>
      

      I have tried something like search: “(<p.*?)(?<!<\/p>)\R +” and replacing it with "\1 ", but besides the fact that it won’t do multiple lines at once (I have to keep repeating the search/replace), the main issue is that if there is a line that starts with a <p> and ends with a </p>, it matches the whole line and whatever comes after it.

      What’s the best way to construct this search and replace?

      Terry RT 1 Reply Last reply Reply Quote 0
      • Mark OlsonM
        Mark Olson
        last edited by Mark Olson

        @pbarney

        This is a classic case of guy038’s patented “replace in a specific zone of text” regex (follow link for an explanation of how this regex works).

        Simply replace (?s-i)(?:<p>|(?!\A)\G)(?:(?!</p>).)*?\K\R with nothing.

        Essentially, everything after the \K is what you want to remove, and everything before the \K is ensuring that the replacements only happen within <p> tags.

        1 Reply Last reply Reply Quote 4
        • Terry RT
          Terry R @pbarney
          last edited by

          @pbarney said in How to combine a multi-line html element into one line?:

          What’s the best way to construct this search and replace?

          A similar question was posted some time ago here. I suggest reading that. As you seem to be proficient at regex hopefully you can adjust to suit your need.

          Terry

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