Community
    • Login

    Reg expression to change characters within brackets '[ ]' to uppercase?

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    4 Posts 3 Posters 1.6k 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.
    • Kurt EhrhardtK
      Kurt Ehrhardt
      last edited by

      Hi,
      I’m having trouble coming up with a reg expression for the replace option for the following scenario:

      Would like to replace all lowercase characters with brackets to uppercase.

      i.e. array[enum1_name] becomes array[ENUM1_NAME].

      I’ll defer to the reg expression experts out there.

      Thanks!

      1 Reply Last reply Reply Quote 0
      • PeterJonesP
        PeterJones
        last edited by PeterJones

        Find what: \[([^]]*)\]
        Replace With: [\U$1\E]

        Finds everything between [ and ], inclusive, saving the stuff between the brackets as $1
        The \U and \E say to make everything between those two escapes uppercase. Thus \U$1\E uppercases everything in $1.

        EDIT: that assumes you don’t have nested brackets, like outer[ inner[ ENUM_HERE ] ]. If you do, you’ll have to define some rules for what’s allowed and what isn’t, and what needs capitalizing and what doesn’t.

        1 Reply Last reply Reply Quote 1
        • Kurt EhrhardtK
          Kurt Ehrhardt
          last edited by

          Thanks so much! Worked like a champ! Over 750 replacements in one file alone. What a time saver.

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

            Hi, @kurt-ehrhardt, @Peterjones and All

            Ah, already solved, nice :-)) So, just for fun, an other formulation could be :

            SEARCH \[.*?\]

            REPLACE \U$0

            Note, Kurt, that the two search regexes, from Peter and me, would match a “multi-lines” square-bracket block, too, as, for instance :

            array[ENUM1
            _NAME]
            

            Thus, to restrict to a single-line square-bracket block, the Peter’s search regex should be changed into :

            SEARCH \[([^]\r\n]*)\]

            And mine should be written :

            SEARCH (?-s)\[.*?\]

            Cheers,

            guy038

            P.S. :

            Note that replacement case operations occur, only, with classical letters in range [A-Za-z] Unfortunately, for Non English-American people, this does not work, at all, for accentuated letters :-((

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