Community
    • Login

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

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    4 Posts 3 Posters 1.9k Views 2 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.
    • Kurt EhrhardtK Offline
      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 Offline
        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 Offline
          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 Offline
            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

            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