Community
    • Login

    Conditional Replace Char

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    3 Posts 2 Posters 262 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.
    • João BorlothJ
      João Borloth
      last edited by João Borloth

      Hey guys.
      I need technical help.
      Here’s the thing: a specific character appears multiple times in the text.
      I need to change (replace) it for another character, but only if it doesn’t have a (blank) space before and after, but rather with one other different…
      Text example (original):

      Verbo1 - Diga-lhe.
      Verbo1 - Verbo2 - Diga-lhe. Fale-nos.
      Verbo1 - Verbo2 - Verbo3 - Verbo4 - Diga-lhe. Fale-nos. Veja-as. Mande-nos.

      In the 3-line text above, if I change (replace) “-” (hyphen) to “,” (comma), will swap all hyphens. But I want you to change(replace/swap) only the ones that are NOT in between space, so the new text (end result) is:

      Verbo1 - Diga,lhe.
      Verbo1 - Verbo2 - Diga,lhe. Fale,nos.
      Verbo1 - Verbo2 - Verbo3 - Verbo4 - Diga,lhe. Fale,nos. Veja,as. Mande,nos.

      Notice, replaced only the hyphens that are in the middle of the words.
      The ones that are “loose” or “in the void”, have not been modified.
      That’s what I need.

      How do I do this?

      Thanks

      1 Reply Last reply Reply Quote 0
      • CoisesC
        Coises
        last edited by

        Find what: \b-\b
        Replace with: ,

        and select Regular expression at the bottom, then click Replace All.

        Explanation: \b represents a “word boundary” — a place where a word character (a letter, a digit or an underscore) and a non-word character are adjacent. The hyphens you want to replace have word boundaries on both sides since the hyphen is not a word character, and the characters on either side are. The hyphens you don’t want to replace do not have word boundaries on either side, since neither the hyphen nor the space is a word character.

        João BorlothJ 1 Reply Last reply Reply Quote 4
        • João BorlothJ
          João Borloth @Coises
          last edited by

          @Coises , thanks.
          Worked perfectly.
          I did a test, using a letter and not the hyphen.
          In this case, it was exactly the opposite, replacing the isolated letter, not the letter in the middle of the word. So here we have a new trick: finding loose letters in the text.

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