Community
    • Login

    convert lower to upper and upper to lower aftert colon

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    12 Posts 3 Posters 2.0k 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.
    • real_1bxR
      real_1bx
      last edited by real_1bx

      ** hi, i have text file like:

      john:abc232
      smith:Snlf1999
      

      i want it to be

      john:Abc232
      smith:snlf1999
      

      i used
      to search

      (?-s)(?<=:)(.)(.*?)
      

      and replace with

      \u\1\L\2
      

      and this did the job for change all to upper case, now i want to go back to lowercase but i couldn’t find a way.

      also, the main goal was to convert lower to upper and upper to lower in the same time.

      Thanks in advance. **

      PeterJonesP 1 Reply Last reply Reply Quote 1
      • PeterJonesP
        PeterJones @real_1bx
        last edited by PeterJones

        @real_1bx ,

        Thanks for showing what you have, what you want, and what you’ve tried. That makes it so much nicer to try to help you.

        If I am understanding the logic, you want colon-lowercase to become colon-uppercase, and colon-uppercase to become colon-lowercase, and everything after that to be made lowercase, right? If so, then I think this will work for you:

        • FIND = (?-is)(?<=:)(?:([a-z])|([A-Z]))(.*)
        • REPLACE = (?1\u$1)(?2\l$2)\L$3\E
        • MODE = regular expression
        • REPLACE ALL (with the look behind, replace-one-at-a-time doesn’t seem to work)

        My thought process: I want it to be able to differentiate lowercase and uppercase in two different “if” conditions, so wrap the first character after the colon as an alternation, with group1 being lowercase, group2 being uppercase; then put everything else (greedily, so it doesn’t end up empty) in group3. In the replacement, if group1 (lowercase) matched, then uppercase it; if group2 (uppercase) matched, then lowercase it; no matter what, lowercase everything in group3.

        with the example text

        john:abc232
        smith:Snlf1999
        john:aBC232
        smith:SNLF1999
        

        it will become

        john:Abc232
        smith:snlf1999
        john:Abc232
        smith:snlf1999
        

        I am surprised @guy038 hadn’t chimed in already, since he was around a few minutes ago. I am sure at some point, he will offer a more elegent, well-explained version. :-)

        PeterJonesP real_1bxR 2 Replies Last reply Reply Quote 3
        • PeterJonesP
          PeterJones @PeterJones
          last edited by

          FYI:

          The official docs have a section on substitution regex, but the conditional replacements were not explained: https://npp-user-manual.org/docs/searching/#substitutions

          The most recent update to the documentation github has more details on that; it will be in the next release to the doc website (whenever that occurs): https://github.com/notepad-plus-plus/npp-usermanual/blob/852b4ac8a2e667be027d3f7db0a04cfeb2d71eca/content/docs/searching.md#substitution-conditionals

          1 Reply Last reply Reply Quote 1
          • real_1bxR
            real_1bx @PeterJones
            last edited by

            @PeterJones said in convert lower to upper and upper to lower aftert colon:

            If I am understanding the logic, you want colon-lowercase to become colon-uppercase, and colon-uppercase to become colon-lowercase, and everything after that to be made lowercase, right? If so, then I think this will work for you:

            Thank you very much, i really do appropriate taking time to help me out.
            the logic is i want colon-lowercase to become colon-uppercase, and colon-uppercase to become colon-lowercase but everything after that stay the same i don’t want change it.

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

              Hello @real_1bx and All,

              You need to distinguish an uppercase letter from a lowercase letter !

              So, I think that the following regex S/R should work !

              SEARCH (?-is):(([A-Z])|[a-z])(.+)

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

              For instance, the text :

              john:abC232
              smith:SnLf1999
              

              is changed into :

              john:Abc232
              smith:snlf1999
              

              Notes :

              • First, the in-line modifiers (?-is) means that :

                • The search will be carried on, in a non-insensitive way ( -i )

                • Any regex dot symbol matches a single standard character only ( and not line-break chars )

              • Groups involved in the search regex are :

                • Group 1 = ([A-Z])|[a-z], so the first letter after the : char, which may be, either a lower-case or an upper-case letter

                • Group 2 = [A-Z], so the first upper-case letter after the :

                • Group 3 = .+, so all the remaining characters of current line, after the : and a first letter

              • In the replacement regex :

                • : rewrites the colon, first

                • Then, the (?2\l:\u) syntax is a conditional structure which forces the next character to be written :

                  • In lower case if group 2 exists, that is to say if an upper case letter has matched

                  • In upper case if group 2 does not exist => a lower case letter has matched

                • \1 is the first letter whose case has been modified

                • Finally, \L\3 rewrites all the remaining characters, of current line, in lower-case

              • Note that I do not use the look-behind structure (?<=:) which enable us to use the step by step replacement with repeated hits on the Replace button

              Best Regards,

              guy038

              @peterjones said :

              I am surprised @guy038 hadn’t chimed in already, since he was around a few minutes ago.

              Well, I was chatting for a moment with my son who’s going back to Lyon ;-)

              1 Reply Last reply Reply Quote 2
              • PeterJonesP
                PeterJones @real_1bx
                last edited by

                @real_1bx said in convert lower to upper and upper to lower aftert colon:

                Thank you very much, i really do appropriate taking time to help me out.
                the logic is i want colon-lowercase to become colon-uppercase, and colon-uppercase to become colon-lowercase but everything after that stay the same i don’t want change it.

                Ahh, your \L confused me to thinking you wanted the rest lowercase.

                Just get rid of the third group in find and replace should do it:

                FIND = (?-is)(?<=:)(?:([a-z])|([A-Z]))
                REPLACE = (?1\u$1)(?2\l$2)
                MODE = regular expression

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

                  Hi, @real_1bx, @peterjones and All,

                  So, I did the same mistake as Peter !

                  Thus, my regex S/R should be modified as :

                  SEARCH (?-i):(([A-Z])|[a-z])

                  REPLACE :(?2\l:\u)\1

                  Cheers,

                  guy038

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

                    @real_1bx, @peterjones and All,

                    Last news : we don’t even need an outer group to capture the letter ! So, why not this attempt :

                    SEARCH (?-i)(:[A-Z])|:[a-z]

                    REPLACE (?1\L:\U)$0


                    So the following text :

                    john:abC232
                    smith:SnLf1999
                    

                    becomes :

                    john:AbC232
                    smith:snLf1999
                    

                    BR,

                    guy038

                    real_1bxR 1 Reply Last reply Reply Quote 2
                    • real_1bxR
                      real_1bx @guy038
                      last edited by

                      @guy038
                      Thank you very much , that’s saved me a LOT of time.

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

                        Hello @real_1bx,

                        I hope that you noticed why I use, this time, the (?1\L:\U)$0 syntax ( and not (?1\l:\u)$0 ), in replacement ?

                        Just because the string to convert in upper / lower case ( $0 ) is two chars long ( a colon : + a letter ) !

                        BR

                        guy038

                        real_1bxR 1 Reply Last reply Reply Quote 1
                        • real_1bxR
                          real_1bx @guy038
                          last edited by

                          @guy038 is it possible to delete whole line if there is number after the :
                          i mean the first character after colon not the whole word.
                          like

                          abc:fkls
                          john:13kkmsd
                          smith:kmskl
                          ```
                          **to be**
                          
                          ```
                          abc:fkls
                          smith:kmskl
                          ```
                          1 Reply Last reply Reply Quote 0
                          • guy038G
                            guy038
                            last edited by guy038

                            Hi, @real_1bx, and All,

                            This time, due to the simultaneous search of an entire line, with its line-break, when a digit follows the colon char, we need, again, two groups, leading up to that regex S/R :

                            SEARCH (?-is)^.+:\d.+\R|((:[A-Z])|:[a-z])

                            REPLACE ?1(?2\L:\U)\1

                            For instance, the input text, below :

                            john:abC232
                            john:13kkmsd
                            smith:SnLf1999
                            

                            would be modified as :

                            john:AbC232
                            smith:snLf1999
                            

                            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