Community
    • Login

    RegEx assistance for adding a space before an Upper case letter

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    2 Posts 2 Posters 61 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.
    • Brenda Gross 0B
      Brenda Gross 0
      last edited by

      I have hundreds of table column names that contain no embedded spaces. Some of the column names use an _ to separate words, some use camel case, some use a mixture. Replacing the _ with a space is easy. But how to add a space prior to any upper case character that is not the first character and is not already preceded by a space? Each column name will be on a separate line. Thanks for any help

      Example:
      Average_Balance
      SystemRate_Change
      DateClosed

      Desired Results:
      Average Balance
      System Rate Change
      Date Closed

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

        Hello, @brenda-gross-0 and All,

        Very easy with regexes !


        • Open the Replace dialog ( Ctrl + H )

        • Un-tick all box options

        • FIND (?-i)(?<=\l)(?=\u)|_

        • REPLACE \x20

        • Tick the Wrap around option

        • Select the Regular expression search mode

        • Click, once only, on the Replace All button

        Voila !


        Notes :

        • The leading part (?-i) is an in-line modifier which force to take care about the case of letters, meaning non-insensitive search !

        • Then, due to the alternation symbol |, two individual regexes are performed, simultaneously :

          • (?<=\l)(?=\u) which searches for an empty string between a lower-case letter and an upper-case letter

          • _ which simply searches for the underline character

        • In both cases, the replacement changes the regex search with an normal space character \x20. I could have chosen to type a single space char, instead !

        • The regex search may also be expressed as a non-capturing group : (?-i:(?<=\l)(?=\u)|_), with the leading non-insensitive option

        Best Regards,

        guy038

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