Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - Replace

Reply
 
Old 10-13-2007   #1 (permalink)
tb


 
 

Replace

Hej Powershell team.

In gui-help replace it can be defined as - a specified number of
times.
But how should i do, if i only will change the first space and not the
other space
in the following example ???

${C:\powerstart.txt} -replace ' ',';' > C:\powerstart1.txt

I need a real reference manual !!
--------------------------------------------------------------------

Replace

Definition: Returns a string in which a specified substring has been
replaced
with another substring a specified number of times.

$a = "bxnxnx"
$a = $a -replace("x","a")

Regards
Torben Brønsholm
Denmark


My System SpecsSystem Spec
Old 10-13-2007   #2 (permalink)
Shay Levi


 
 

Re: Replace


The String.Replace constructor takes only 2 parameters :

PS C:\Scripts> "bxnxnx" | gm rep*


TypeName: System.String

Name MemberType Definition
---- ---------- ----------
Replace Method System.String Replace(Char oldChar, Char newChar), System.String
Replace(String oldValue, String newValue)


Use the Regex.Replace Method instead (http://msdn2.microsoft.com/en-us/lib...31(VS.71).aspx

This will replace only the first and second occurrences of "x" in "bxnxnx"

PS > $r=[regex]'x';
PS > $r.Replace("bxnxnx","a",2)
bananx






Shay
http://scriptolog.blogspot.com


Quote:

> Hej Powershell team.
>
> In gui-help replace it can be defined as - a specified number of
> times.
> But how should i do, if i only will change the first space and not the
> other space
> in the following example ???
> ${C:\powerstart.txt} -replace ' ',';' > C:\powerstart1.txt
>
> I need a real reference manual !!
> --------------------------------------------------------------------
> Replace
>
> Definition: Returns a string in which a specified substring has been
> replaced
> with another substring a specified number of times.
> $a = "bxnxnx"
> $a = $a -replace("x","a")
> Regards
> Torben Brønsholm
> Denmark

My System SpecsSystem Spec
Old 10-13-2007   #3 (permalink)
Shay Levi


 
 

Re: Replace

The same applies for the -Replace operator

PS C:\Scripts> "bxnxnx" -Replace("x","a",2)
The -replace operator only permits 2 elements on the right-hand side, not 3.
At line:1 char:18
+ "bxnxnx" -Replace( <<<< "x","a",2)

So, regex is your best friend in this case.

Shay
http://scriptolog.blogspot.com


Quote:

> The String.Replace constructor takes only 2 parameters :
>
> PS C:\Scripts> "bxnxnx" | gm rep*
>
> TypeName: System.String
>
> Name MemberType Definition
> ---- ---------- ----------
> Replace Method System.String Replace(Char oldChar, Char newChar),
> System.String
> Replace(String oldValue, String newValue)
> Use the Regex.Replace Method instead
> (http://msdn2.microsoft.com/en-us/lib...31(VS.71).aspx
>
> This will replace only the first and second occurrences of "x" in
> "bxnxnx"
>
PS>> $r=[regex]'x';
PS>> $r.Replace("bxnxnx","a",2)
Quote:

> bananx
>
> Shay
> http://scriptolog.blogspot.com
Quote:

>> Hej Powershell team.
>>
>> In gui-help replace it can be defined as - a specified number of
>> times.
>> But how should i do, if i only will change the first space and not
>> the
>> other space
>> in the following example ???
>> ${C:\powerstart.txt} -replace ' ',';' > C:\powerstart1.txt
>> I need a real reference manual !!
>> --------------------------------------------------------------------
>> Replace
>>
>> Definition: Returns a string in which a specified substring has been
>> replaced
>> with another substring a specified number of times.
>> $a = "bxnxnx"
>> $a = $a -replace("x","a")
>> Regards
>> Torben Brønsholm
>> Denmark

My System SpecsSystem Spec
Old 10-13-2007   #4 (permalink)
Shay Levi


 
 

Re: Replace

BTW, to replace all occurrences of "x" use a negative number (or just omit
the third argument)

$r=[regex]'x';
$r.Replace("bxnxnx","a",-1)
banana

Shay
http://scriptolog.blogspot.com


Quote:

> The String.Replace constructor takes only 2 parameters :
>
> PS C:\Scripts> "bxnxnx" | gm rep*
>
> TypeName: System.String
>
> Name MemberType Definition
> ---- ---------- ----------
> Replace Method System.String Replace(Char oldChar, Char newChar),
> System.String
> Replace(String oldValue, String newValue)
> Use the Regex.Replace Method instead
> (http://msdn2.microsoft.com/en-us/lib...31(VS.71).aspx
>
> This will replace only the first and second occurrences of "x" in
> "bxnxnx"
>
PS>> $r=[regex]'x';
PS>> $r.Replace("bxnxnx","a",2)
Quote:

> bananx
>
> Shay
> http://scriptolog.blogspot.com
Quote:

>> Hej Powershell team.
>>
>> In gui-help replace it can be defined as - a specified number of
>> times.
>> But how should i do, if i only will change the first space and not
>> the
>> other space
>> in the following example ???
>> ${C:\powerstart.txt} -replace ' ',';' > C:\powerstart1.txt
>> I need a real reference manual !!
>> --------------------------------------------------------------------
>> Replace
>>
>> Definition: Returns a string in which a specified substring has been
>> replaced
>> with another substring a specified number of times.
>> $a = "bxnxnx"
>> $a = $a -replace("x","a")
>> Regards
>> Torben Brønsholm
>> Denmark

My System SpecsSystem Spec
Old 10-13-2007   #5 (permalink)
Marco Shaw [MVP]


 
 

Re: Replace

> PS > $r=[regex]'x';

And this will do a space:
$r=[regex]' '

In some cases, (much) more complex regular expressions are required to
match things just the way you want it.

Marco
My System SpecsSystem Spec
Old 10-13-2007   #6 (permalink)
Steven Hystad


 
 

Re: Replace

The replace operator supports regex.
To just replace the first space use a regex that only selects the first
space. i.e. '^ '
e.g. $a -replace '^ ',';' | output

<tb@xxxxxx> wrote in message
news:1192269312.197948.307570@xxxxxx
Hej Powershell team.

In gui-help replace it can be defined as - a specified number of
times.
But how should i do, if i only will change the first space and not the
other space
in the following example ???

${C:\powerstart.txt} -replace ' ',';' > C:\powerstart1.txt

I need a real reference manual !!
--------------------------------------------------------------------

Replace

Definition: Returns a string in which a specified substring has been
replaced
with another substring a specified number of times.

$a = "bxnxnx"
$a = $a -replace("x","a")

Regards
Torben Brønsholm
Denmark

My System SpecsSystem Spec
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
Old 10-14-2007   #8 (permalink)
Kiron


 
 

Re: Replace

I think that would be a good suggestion to put up in Connect. Maybe there is still time left to include a third element, the repetition element default to -1, in the -replace operator for v2.

--
Kiron
My System SpecsSystem Spec
Old 10-14-2007   #9 (permalink)
MichielV


 
 

RE: Replace

Expresso (http://www.ultrapico.com/Expresso.htm) is a great tool for working
with regular expressions. It can explain existing regular expressions or it
can help you develop your own.

regards

Michiel

"tb@xxxxxx" wrote:
Quote:

> Hej Powershell team.
>
> In gui-help replace it can be defined as - a specified number of
> times.
> But how should i do, if i only will change the first space and not the
> other space
> in the following example ???
>
> ${C:\powerstart.txt} -replace ' ',';' > C:\powerstart1.txt
>
> I need a real reference manual !!
> --------------------------------------------------------------------
>
> Replace
>
> Definition: Returns a string in which a specified substring has been
> replaced
> with another substring a specified number of times.
>
> $a = "bxnxnx"
> $a = $a -replace("x","a")
>
> Regards
> Torben Brønsholm
> Denmark
>
>
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
replace PowerShell
Remove oem and replace with another oem Vista General
Replace IE7 with IE6? Vista General
Replace sed? PowerShell
Using .replace PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46