Split string in PowerShell

I have heard that this forum amazes with the answer to the craziest questions and I have searched hi and low for an answer to my crazy question, however I cannot find the answer. So I'm passing this on to the community.

I am using PowerShell to execute my scripts. please do not offer me a solution in another scripting language, I am sure other script languages ​​will do this, however I need this in PowerShell.

I have many lines that I need to break, they are similar in nature to:

HelloWorld
HelloWorldIAmNew
HelloWorldIAmNewToScripting
ThankYouForHelpingMe

      

I need to break them down based on Capital Letters.

Hello World
Hello World I Am New
Hello World I Am New To Scripting
Thank You For Helping Me

      

I have a basic understanding of line splitting, but it is more complex than your middle line.

+3


source to share


2 answers


Simple enough to do using a regex with negative and positive lookahead (?=pattern)

and a case sensitive operator -csplit

, for example:

PS>  "HelloWorldIAmNewToScripting" -csplit "(?<=.)(?=[A-Z])"
Hello
World
I
Am
New
To
Scripting

      



Or, if you want the space to be split:

PS>  "$("HelloWorldIAmNewToScripting" -csplit "(?<=.)(?=[A-Z])")"
Hello World I Am New To Scripting

      

+9


source


Try the following:

("HelloWorldIAmNewToScripting" -creplace '[A-Z]', ' $&').Trim().Split($null)
Hello
World
I
Am
New
To
Scripting

      



or

("HelloWorldIAmNewToScripting" -creplace '[A-Z]', ' $&').Trim()
Hello World I Am New To Scripting

      

+1


source







All Articles