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 - Search and replace - regex problem

Reply
 
Old 06-19-2007   #1 (permalink)
dandbnews@talktalk.net


 
 

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 SpecsSystem Spec
Old 06-19-2007   #2 (permalink)
Kiron


 
 

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 SpecsSystem Spec
Old 06-19-2007   #3 (permalink)
dandbnews@talktalk.net


 
 

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 SpecsSystem Spec
Old 06-21-2007   #4 (permalink)
dandbnews@talktalk.net


 
 

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 SpecsSystem Spec
Old 06-21-2007   #5 (permalink)
Keith Hill [MVP]


 
 

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 SpecsSystem Spec
Old 06-21-2007   #6 (permalink)
dandbnews@talktalk.net


 
 

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 SpecsSystem Spec
Old 06-21-2007   #7 (permalink)
Kiron


 
 

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 SpecsSystem Spec
Old 06-21-2007   #8 (permalink)
Keith Hill [MVP]


 
 

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 SpecsSystem Spec
Reply

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


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