Iverson Wang
Iverson Wang

Reputation: 187

break a long string into several lines in Powershell

I try to do substring against 1 long string and then want to put each sub string into a new line. For example, if I have a long string "1pxxxx2pxxx1pyyyyy", the result would be 3 lines as below.

1pxxxx
2pxxx
1pyyyyy

In my case, 1p and 2p are pre-defined keyword. I really appreciate any helps. Thank you.

Upvotes: 1

Views: 209

Answers (1)

mklement0
mklement0

Reputation: 437197

Use -split, the string-splitting operator, with a positive lookahead assertion (more details here):

PS> '1pxxxx2pxxx1pyyyyy' -split '(?=1p|2p)' -ne ''
1pxxxx
2pxxx
1pyyyyy

Upvotes: 3

Related Questions