Community
    • Login

    Case on/at replace.

    Scheduled Pinned Locked Moved General Discussion
    7 Posts 4 Posters 399 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

      How do I replace one word with another, “respecting” the case?
      example:
      (I want the following result:)

      Screw => Nail
      SCREW => NAIL
      screw => nail
      ScreW => NaiL
      sCREW => nAIL

      If I use “ignore case”, all replaced Word Will at same.

      Example:
      Locate: screw
      Replace with: nAil

      I want the following result:

      Screw => nAil
      SCREW => nAil
      sCREw => nAil

      Thanks

      PeterJonesP CoisesC 2 Replies Last reply Reply Quote 0
      • PeterJonesP
        PeterJones @João Borloth
        last edited by PeterJones

        @João-Borloth ,

        I think you messed up your final “I want the following result”, because all three lines resulted in nAil in your example, which is not what I thought you wanted. I think maybe you meant “I would get the following result, which is not what I want” for that last one.

        It’s not easy to do in Notepad++ – and in my opinion, the complexity increases to the point of being impractical in native Notepad++.

        One might make a really complicated regular expression that might be able to detect the case using lots of conditionals and really fancy capture groups for the match, then lots and lots and lots of conditionals. But it’s not something that would be reasonable to implement, and it would probably not work on the next pair of words you tried it on.

        Slightly more promising would be to write a script in one of the scripting plugins (like PythonScript or LuaScript plugins). But most of the work involved would be deciding on an algorithm for how to map the case of an N letter input to the case for an M letter output, when M != N (and such an algorithm is not something that has anything to do with Notepad++, so it’s off topic here; if you had the algorithm, but just needed help in doing the hooks to read the words from the active Notepad++ editor window, that’s something we can help you with, if you have a specific question). And really, you wouldn’t be using the major features of the scripting-plugin overhead (access to the Notepad++ GUI commands, and similar), so it would be just as easy to write the code in your favorite programming language to read and edit the file, and not try to run it with the scripting plugin (and at that point, it’s just coding in your favorite programming language, so it’s off-topic here, since we aren’t a general-purpose coding support or code-writing service).

        So my answer is “it’s doubtful I could generate a satisfactory regex that wouldn’t disappoint you within a couple pairs of words; I think that you really need to create a custom script/program to do it; and even then, we’re not a code-writing service”. If someone else disagrees with me, they can feel free to chime in with a regex that works (but it’s doubtful to me that it would “work” in the most generic case that I’m thinking you want). Someone might have a better idea than I have for a regex solution, but I want to manage your expectations.

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

          @João-Borloth said in Case on/at replace.:

          Screw => Nail
          SCREW => NAIL
          screw => nail
          ScreW => NaiL
          sCREW => nAIL

          I can’t be certain, but it looks like what you describe as “respecting case” means, precisely:

          • The case of the initial letter must be preserved.
          • The case of the final letter must be preserved.
          • The remaining letters are assumed to be all the same case, which should be preserved.

          It is unclear how text like ScReW should be replaced. For the following, I will assume the case of the second letter in the word will control the case of all replacement letters except the first and the last (so ScReW => NaiL).

          You could use this:

          Find what: (?-i)(?:(S)|s)(?:(C)|c)(?i:re)(?:(W)|w)
          Replace with: (?1N:n)(?2AI:ai)(?3L:l)

          to accomplish what you want.

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

            Thanks for the quick responses!
            I imagined something more generic, to use with any words.
            A “false” or unrealistic example, just for example, would be:
            locate: ($%1)[screw](&#1)
            replace with: ($%1)[nail]($%&2)


            A greater need would be the case of just the 1st letter and all the others.

            example:

            Screw => Nail
            sCREW => nAIL
            screw => nail
            SCREW => NAIL
            

            notice, only the 1st gear changes and all the others are the same

            —

            moderator added code markdown around text; please don’t forget to use the </> button to mark example text as “code” so that characters don’t get changed by the forum, and regexes can go inside pairs of ` (like `redtext`) to make them redtext (and so forum formatting won’t eat characters from those, either)

            CoisesC 1 Reply Last reply Reply Quote 0
            • CoisesC
              Coises @João Borloth
              last edited by Coises

              @João-Borloth said in Case on/at replace.:

              I imagined something more generic, to use with any words.

              What I suggested can be used with any words. Is it not obvious how to modify the expression for any other pair of words? Split the first word into four parts:

              1. The first letter
              2. The second letter
              3. The third through next-to-last letter
              4. The last letter

              Each of 1, 2 and 4 becomes (?:(upper)|lower) while 3 becomes (?i:letters) in the find expression.

              Split the second word into three parts:

              1. The first letter
              2. The second through next-to-last letter
              3. The last letter

              These become (?nUPPER:lower) in the replacement.

              If what you mean is that you have a long list of these words and you want to be able to paste them into an expression without having to break them up into parts and include both upper and lower case forms — I can’t think of a way to accomplish that with a regular expression. Maybe someone else can.

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

                Thanks for your help.
                I’m new to this program, and I haven’t learned how to conduct regex

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

                  Hello, @joão-borloth, @peterjones, @coises and All,

                  @joão-borloth, here is my contribution to your problem ! I assume these following statements :

                  • Regarding the searched expression :

                    • The entire searched expression begins and ends each line and contains, at least, three characters

                    • The searched expression begins with two letter characters and ends with a letter character

                    • The characters of the searched expression, from the third one to the last but one, may be any char

                  • Regarding the replacement expression :

                    • The replacement expression contains, at least, two characters

                    • The replacement expression begins with a letter character and ends with a letter character

                    • The characters of the replacement expression, from the second one to the last but one, may be any char

                  • Regarding the case rules :

                    • The case of the first letter of the replacement expression will be the same as the case of the first letter of the searched expression

                    • The case of the last letter of the replacement expression will be the same as the case of the last letter of the searched expression

                    • The case of the all other characters of the replacement expression will be the same as the case of the second letter of the searched expression


                  First, giving your general template :

                  Screw
                  SCREW
                  screw
                  ScreW
                  sCREW
                  

                  I created the same template for two other expressions :

                  Test
                  TEST
                  test
                  TesT
                  tEST
                  
                  Wonderful time
                  WONDERFUL TIME
                  wonderful time
                  Wonderful timE
                  wONDERFUL TIME
                  

                  And using the following regex S/R :

                  • SEARCH (?-is)^(?:(\u)|\l)(?:(\u)|\l).*(?:(\u)|\l)$

                  • REPLACE (?1N:n)(?2AI:ai)(?3L:l)

                  I did get the 3 identical blocks, containing the word nail , below :

                  Nail
                  NAIL
                  nail
                  NaiL
                  nAIL
                  
                  Nail
                  NAIL
                  nail
                  NaiL
                  nAIL
                  
                  Nail
                  NAIL
                  nail
                  NaiL
                  nAIL
                  

                  So, now, the next problem ( which, indeed, is rather the initial problem ! ) is how to get the replacement text, whatever the text used ?

                  Again, we’ll use a regex replacement :

                  • SEARCH (?-s)^(.)(.*)(.)$

                  • REPLACE \(\?1\u\1:\l\1\)\(\?2\U\2\E:\L\2\E\)\(\?3\u\3:\l\3\)

                  For example, given all case syntaxes of the word nail :

                  nail
                  naiL
                  naIl
                  naIL
                  Nail
                  NaiL
                  NaIl
                  NaIL
                  nAil
                  nAiL
                  nAIl
                  nAIL
                  NAil
                  NAiL
                  NAIl
                  NAIL
                  

                  You can verify that we always get the same correct result, below :

                  (?1N:n)(?2AI:ai)(?3L:l)
                  (?1N:n)(?2AI:ai)(?3L:l)
                  (?1N:n)(?2AI:ai)(?3L:l)
                  (?1N:n)(?2AI:ai)(?3L:l)
                  (?1N:n)(?2AI:ai)(?3L:l)
                  (?1N:n)(?2AI:ai)(?3L:l)
                  (?1N:n)(?2AI:ai)(?3L:l)
                  (?1N:n)(?2AI:ai)(?3L:l)
                  (?1N:n)(?2AI:ai)(?3L:l)
                  (?1N:n)(?2AI:ai)(?3L:l)
                  (?1N:n)(?2AI:ai)(?3L:l)
                  (?1N:n)(?2AI:ai)(?3L:l)
                  (?1N:n)(?2AI:ai)(?3L:l)
                  (?1N:n)(?2AI:ai)(?3L:l)
                  (?1N:n)(?2AI:ai)(?3L:l)
                  (?1N:n)(?2AI:ai)(?3L:l)
                  

                  IF, we extend the use of this regex S/R to other expressions, whatever their case, it does give correct results ! For instance, given this short list, below :

                  naiL
                  WINDOW
                  An imPRESSive caSTLe
                  bIg
                  Az
                  

                  We correctly get this OUTPUT :

                  (?1N:n)(?2AI:ai)(?3L:l)
                  (?1W:w)(?2INDO:indo)(?3W:w)
                  (?1A:a)(?2N IMPRESSIVE CASTL:n impressive castl)(?3E:e)
                  (?1B:b)(?2I:i)(?3G:g)
                  (?1A:a)(?2:)(?3Z:z)
                  

                  So, the procedure is :

                  • First, given a specific word, which will be the replacement expression, you’ll find the true replacement regex syntax with :

                    • SEARCH (?-s)^(.)(.*)(.)$

                    • REPLACE \(\?1\u\1:\l\1\)\(\?2\U\2\E:\L\2\E\)\(\?3\u\3:\l\3\)

                  • Secondly, with the following regex S/R :

                    • SEARCH (?-is)^(?:(\u)|\l)(?:(\u)|\l).*(?:(\u)|\l)$

                    • REPLACE : The result of the PREVIOUS regex S/R

                  It would replace any expression with the replacement expression, using our specific case rules !


                  Two examples :

                  • Expression to change : Screw and expression to replace An impressive castle. Thus :

                    • SEARCH (?-is)^(?:(\u)|\l)(?:(\u)|\l).*(?:(\u)|\l)$

                    • REPLACE (?1A:a)(?2N IMPRESSIVE CASTL:n impressive castl)(?3E:e)

                  So, from this INPUT text :

                  Screw
                  SCREW
                  screw
                  ScreW
                  sCREW
                  

                  We would get this OUTPUT :

                  An impressive castle
                  AN IMPRESSIVE CASTLE
                  an impressive castle
                  An impressive castlE
                  aN IMPRESSIVE CASTLE
                  
                  • Expression to change : Wonderful time and expression to replace Az. Thus :

                    • SEARCH (?-is)^(?:(\u)|\l)(?:(\u)|\l).*(?:(\u)|\l)$

                    • REPLACE (?1A:a)(?2:)(?3Z:z)

                  So, from this INPUT text :

                  Wonderful time
                  WONDERFUL TIME
                  wonderful time
                  Wonderful timE
                  wONDERFUL TIME
                  

                  we would get this OUTPUT text :

                  Az
                  AZ
                  az
                  AZ
                  aZ
                  

                  Best Regards,

                  guy038

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