How can I add space before capital letters using a regular expression?

How can I add space before capital letters using a regular expression?

12
Basic UserBasic User
12

    Jan 30, 2019#1

    Hi,

    I have spent a good 3 days to find out a way to do this and started to have a headache. lol

    I want to add space before capital letters using a Perl regex or JavaScript. (I prefer Perl.)

    For example, AbcdeFghiJklmnoPqrstuvwXyz ---> Abcde Fghi Jklmno Pqrstuvw Xyz.

    How can I do this?

    Also, how can I select a portion of the string starting with a capital letter, not ending with a capital letter.

    For instance, how can I select Abcde in the string?

    [:upper:].+(?![:upper:]) , [A-Z].+(?![A-Z]) and [\p{Lu}].+(?!\p{Lu}) didn't work.

    I am just wondering why the 3 regular expressions above don't work.

    I also tried adding the non-greedy operator ? at the end of the 3 regular expressions above.

    I think I can do something if I can select any portion of the string starting with a capital letter, not ending with a capital letter.

    Thank you very much.

    11327
    MasterMaster
    11327

      Jan 30, 2019#2

      1.
      Find what: ([[:upper:]])

      or  (?-i)([A-Z])

      or (?-i)(\p{Lu})

      Replace with:  $1 (There is space before $)

      2.

      Find what: [[:upper:]][[:lower:]]*

      or               (?-i)\p{Lu}\p{Ll}*

      or               (?-i)[A-Z][a-z]*

      See also Perl Regular Expression Syntax.
      It's impossible to lead us astray for we don't care even to choose the way.

      6,685587
      Grand MasterGrand Master
      6,685587

        Jan 30, 2019#3

        It is also possible to use (?<!\s)([A-Z]) and replace string being a space and $1 or \1 and option Match case is checked which is very important as otherwise [A-Z] is equal [A-Za-z]. The lookbehind before the capturing group prevents inserting a space on character left to upper case ASCII letter is already a whitespace character like space, horizontal tab, line-feed, etc.

        (?-i) at beginning of a search expression as written by Ovg disables case-insensitivity defined usually with option Match case. In other words with (?-i) at beginning of a search expression the find option Match case is not effective anymore.
        Best regards from an UC/UE/UES for Windows user from Austria

        19176
        MasterMaster
        19176

          Jan 30, 2019#4

          Or you can use this in case there are more adjacent capital letters :)

          F: (?-i)(?<![A-Z\s])([A-Z]+)
          R: <space>\1

          BTW a little off-topic - UltraEdit version 26.00 uses Boost Perl regular expression library version 1.68. And IDM implemented almost all missing features (backtracking control verbs, named groups in replace, ...). Very nice :)

          12
          Basic UserBasic User
          12

            Jan 30, 2019#5

            Thank you so much Ovg, Mofi, and fleggy.

            I should have checked the match case box. 

            Ah~~```... My 3 days... 

            When will my ignorance go away? lol