![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| 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) |
| | Search and replace - regex problem Hi I want to replace a line in file MySource.cpp as follows. Original line: // HOST COMPILER: ADI VisualDSP++ 4.0 New line: // HOST COMPILER: ADI VisualDSP++ 4.5 I tried the following command: PS C:\Transfer> (gc MySource.cpp) -replace "VisualDSP++ 4.0","VisualDSP ++ 4.5" | sc MySource.cpp but got the following error: Invalid regular expression pattern: VisualDSP++ 4.0. At line:1 char:43 + (gc MySource.cpp) -replace "VisualDSP++ 4.0"," <<<< VisualDSP++ 4.5" | sc MySource.cpp I think that PowerShell does not like the '++' in the match string. I guess '++' has a special meaning in a regular expression. How can I persuade PowerShell to search and replace a string containing '++'? David |
My System Specs![]() |
| | #2 (permalink) |
| | Re: Search and replace - regex problem (gc MySource.cpp) -replace "VisualDSP\+\+ 4.0","VisualDSP++ 4.5" | sc MySource.cpp ....or... (gc MySource.cpp) -replace "VisualDSP\+{2} 4.0","VisualDSP++ 4.5" | sc MySource.cpp Regex Ordinary characters: . $ ^ { [ ( | ) ] } * + ? \ Escape each with a '\' if the matching pattern includes them. '{x}' is called an Explicit Quantifier and specifies exactly 'x' matches. You only have to escape Regex Ordinary characters on the matching pattern, not the substitution pattern, unless the character needs to be escaped in PowerShell. help about_quoting_rules -- Kiron |
My System Specs![]() |
| | #3 (permalink) |
| | Re: Search and replace - regex problem Hi Kiron > Escape each with a '\' if the matching pattern includes them. Thanks for your help, that answers my question. I had tried the PowerShell escape character '`' but had not tried '\'. David |
My System Specs![]() |
| | #4 (permalink) |
| | Re: Search and replace - regex problem I now have a working script which nearly achieves what I want: $pattern = 'ADI VisualDSP\+\+ 4.0' $replacement = 'ADI VisualDSP++ 4.5' foreach ($file in (gci -i *.cpp,*.h,*.asm -r)) { $text = get-content $file if ($text -match $pattern) { $text -replace $pattern, $replacement > $file } } The outstanding problem is that the files are written in unicode. How can I change my script to write in ASCII? David |
My System Specs![]() |
| | #5 (permalink) |
| | Re: Search and replace - regex problem <dandbnews@talktalk.net> wrote in message news:1182430026.559323.233600@n60g2000hse.googlegroups.com... >I now have a working script which nearly achieves what I want: > > $pattern = 'ADI VisualDSP\+\+ 4.0' > $replacement = 'ADI VisualDSP++ 4.5' > foreach ($file in (gci -i *.cpp,*.h,*.asm -r)) > { > $text = get-content $file > if ($text -match $pattern) > { > $text -replace $pattern, $replacement > $file > } > } > > The outstanding problem is that the files are written in unicode. How > can I change my script to write in ASCII? Change this line: $text -replace $pattern, $replacement > $file to $text -replace $pattern, $replacement | Out-File -enc Ascii -append $file You will also need to create the empty file (use new-item) sometime before the foreach loop. -- Keith |
My System Specs![]() |
| | #6 (permalink) |
| | Re: Search and replace - regex problem Hi Keith Thanks for answering my question so quickly. Actually, I want to write back to the same file that was read so my script is now: $pattern = 'HOST COMPILER: ADI VisualDSP\+\+ 4.0' $replacement = 'HOST COMPILER: ADI VisualDSP++ 4.5' foreach ($file in (gci -i *.cpp,*.h,*.asm -r)) { $text = get-content $file if ($text -match $pattern) { $text -replace $pattern, $replacement | Out-File -enc Ascii $file } } and it works fine. One small question on a related note, in Bruce Payette's book on page 318 he uses the command: >> set-content -encoding ascii file$_.txt but if run: >> help set-content -encoding is not listed as a parameter. Why is this? David |
My System Specs![]() |
| | #7 (permalink) |
| | Re: Search and replace - regex problem You're right, never noticed that. It might have been overlooked when writing the help content for Set-Content & Add-Content. (get-help gc).parameters|select -expand parameter|%{$_.name}|sort (get-help gc).parameters|select -expand parameter|?{$_.name -match 'encoding'} (get-help ac).parameters|select -expand parameter|%{$_.name}|sort (get-help sc).parameters|select -expand parameter|%{$_.name}|sort -- Kiron |
My System Specs![]() |
| | #8 (permalink) |
| | Re: Search and replace - regex problem <dandbnews@talktalk.net> wrote in message news:1182433779.950597.267330@o61g2000hsh.googlegroups.com... > Hi Keith > but if run: > >>> help set-content > > -encoding is not listed as a parameter. Why is this? > The current PowerShell doesn't seem to be always consistent with what parameters are available on a cmdlet which is a shame since they have all the necessary metadata to ensure the parameters on a cmdlet help topic are complete. In fact, on PSCX we use the metadata to generate help topic templates to ensure accuracy. Anyway, to get access to this metadate yourself and see what parameters are really available I always use: gcm get-content | fl * Do this and then tell me what parameter is listed that isn't in the help topic? Hint: there's a parameter that provides a feature a number of folks have been asking for lately. :-) -- Keith |
My System Specs![]() |
![]() |
| Thread Tools | |
| |
Similar Threads | ||||
| Thread | Forum | |||
| regex replace function | VB Script | |||
| Replace Start Menu Search with 3rd Party Search | General Discussion | |||
| Re: Call to Regex.Replace is killing all CRLFs | PowerShell | |||
| regex - search - replace question | PowerShell | |||
| Where are things like -replace and split([regex} documented? | PowerShell | |||