![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| 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. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | 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 Specs![]() |
| | #2 (permalink) |
| | 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 Specs![]() |
| | #3 (permalink) |
| | 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.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 Specs![]() |
| | #4 (permalink) |
| | 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.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 Specs![]() |
| | #5 (permalink) |
| | 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 Specs![]() |
| | #6 (permalink) |
| | 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 Specs![]() |
| | #7 (permalink) |
| | 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 Specs![]() |
| | #8 (permalink) |
| | 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 Specs![]() |
| | #9 (permalink) |
| | 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 Specs![]() |
![]() |
| 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 | |||