I'm trying to search a string for some numbers and insert a new line before each, not including the first one.
I don't seem to be able to use the traditional regex \n char to insert a newline. If I use the PowerShell escape character, the regex variable from the set is ignored.
For a given source string of
$theString = "1. First line 2. Second line 3. Third line"
What I want is:
1. First Line 2. Second Line 3. Third line
So I tried this regex to find the numbers 2 to 9 followed by a period and a space:
$theString = $theString -replace '([2-9]\. )','\n$1'
but that yields:
1. First line\n2. Second line\n3. Third line
So I tried using the PowerShell escape character for a newline and put it inside double inverted commas:
$theString = $theString -replace '([2-9]\. )',"`n$1"
but that yields:
1. First Line Second Line Third line
I tried using \r, \r\n, \`r, \`r\`n, etc. trying to force the linebreak, but can't get it to happen without losing the ability to include the current regex variable.