Community
    • Login

    I'm in a Paragraph Find and Replace Hell

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    2 Posts 2 Posters 251 Views 1 Watching
    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.
    • Offshore9521O Offline
      Offshore9521
      last edited by

      Fellow Notepad++ Users,

      Could you please help me the the following search-and-replace problem I am having?

      Im trying to turn the following text into the text mentioned after that. But instead of turning the 5 line text (first one) into the 3 line text (second one), it turns it into a 2 line text.

      Here is the data I currently have (“before” data):

      ‘Colt?’
      He turns to face her, and clears his throat.
      Guilty sign. Very guilty. Probably has a guilty look on his face, but you
      can’t tell, because he’s still wearing that helmet so you can’t see his
      eyes.
      

      Here is how I would like that data to look (“after” data):

      ‘Colt?’
      He turns to face her, and clears his throat.
      Guilty sign. Very guilty. Probably has a guilty look on his face, but you can’t tell, because he’s still wearing that helmet so you can’t see his eyes.
      

      The “2 line text”:

      ‘Colt?’ He turns to face her, and clears his throat.
      Guilty sign. Very guilty. Probably has a guilty look on his face, but you can’t tell, because he’s still wearing that helmet so you can’t see his eyes.
      

      To accomplish this, I have tried using the following Find/Replace expressions and settings

      • Find What = (?<![\.\!\?])\r\n([a-z])
      • Replace With = \1
      • Search Mode = REGULAR EXPRESSION
      • Dot Matches Newline = NOT CHECKED

      Unfortunately, this did not produce the output I desired, and I’m not sure why. Could you please help me understand what went wrong and help me find the solution?

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

        Hello @offshore9521 and All,

        Humm…, @offshore9521, there are two separate problems with your regex !

        From your INPUT text :

        ‘Colt?’
        He turns to face her, and clears his throat.
        Guilty sign. Very guilty. Probably has a guilty look on his face, but you
        can’t tell, because he’s still wearing that helmet so you can’t see his
        eyes.
        

        Your regex (?<![\.\!\?])\r\n([a-z]) means :

        • If a \r\n sequence, followed with a letter (a through z), is not preceded by :

          • A dot

          • An exclamation mark

          • An interrogation mark

        • Then rewrite this letter


        BTW, you can shorter this regex as (?<![.!?])\r\n([a-z])

        But, the ’ character is not a character of the [.!?] list. Thus, this explains why your regex merges the lines 1 and 2


        Now, I suppose that your true goal was : if a line is not a true sentence, then merge that line with the next one with a space character ? This lead to the following regex S/R :

        FIND (?<![.!?’])\r\n(?=\w)

        REPLACE \x20

        So, any \r\n sequence, which respects the before and after conditions, is simply replaced with a space character


        Notes :

        • As you see, I included the ’ character within the list of the forbidden chars, in the negative look-behind

        • I used \w, which is identical to the [\u\l\d_] class character, instead of [a-z] to not bother about case !

        Remark :

        • Instead of \r\n, I could have used the \R syntax with matches any kind of line endings ( \r, \n or \r\n ), but, because it is preceded by a negative look-behind, we must insert \r and \n as forbidden characters, as well !

        FIND (?<![.!?’\r\n])\R(?=\w)

        REPLACE \x20

        Best Regards,

        guy038

        P.S. : As an exercice, try to understand why the following regex S/R does not work as expected :

        FIND (?<![.!?’])\R(?=\w)

        REPLACE \x20

        To help you, don’t forget to click on the ¶ icon of the Toolbar !

        1 Reply Last reply Reply Quote 2

        Hello! It looks like you're interested in this conversation, but you don't have an account yet.

        Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

        With your input, this post could be even better 💗

        Register Login
        • First post
          Last post
        The Community of users of the Notepad++ text editor.
        Powered by NodeBB | Contributors