Thread: Replace
View Single Post
Old 10-14-2007   #7 (permalink)
Kiron


 
 

Re: Replace

Here are two match patterns that create three matched groups. The second group is the first space found. The replace pattern consists of the first group, the character that will replace the second group and the third group. Try it:

$pattern1 = '(.*?)( )(.*)'
$pattern2 = '([^ ]*?)( )(.*)'

" 1 2 3 " -replace $pattern1, '$1;$3'
"1 2 3 " -replace $pattern1, '$1;$3'
"12 3 " -replace $pattern1, '$1;$3'
"123 " -replace $pattern1, '$1;$3'

" 1 2 3 " -replace $pattern2, '$1;$3'
"1 2 3 " -replace $pattern2, '$1;$3'
"12 3 " -replace $pattern2, '$1;$3'
"123 " -replace $pattern2, '$1;$3'

--
Kiron
My System SpecsSystem Spec