The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

SimonM (4) [Avatar] Offline
#1
Hi there,

Am working my way through the book and have a real world problem relating to chapter 2 (OK I havent got very far in the book yet)!

I want to remove parentheses from a string e.g. "(R)", but Powershell always evaluates the parentheses as an expression rather than as part of the string.

e.g.

I want to transform "Windows (R)" into "Windows OK", but I cant get it:

$a="Windows (R)"
$a=$a -replace("(R)","OK")
$a
Windows (OK)

I've tried using single quotes
$a=$a -replace('(R)','OK')
$a
Windows (OK)

Or the escape character
$a=$a -replace("`(R)","OK")
$a
Windows (OK)

But I cant get rid of the brackets from the string!

Can you help?

Thanks a lot,

Simon.
Shay (10) [Avatar] Offline
#2
Re: How to remove Parentheses "()" from a string
Parentheses are special regular expression characters, you need to escape them :


$a="Windows (R)"
$a=$a -replace "(R)","OK"
$a

Windows OK

-----
Shay Levi
$cript Fanatic
http://scriptolog.blogspot.com
SimonM (4) [Avatar] Offline
#3
Re: How to remove Parentheses "()" from a string
Thats great, thanks a lot, that works perfectly.

So is another escape character, different from `, how strange.

Is there any help or documentation about this anywhere, would like to understand a bit more about why this is so?

Thanks,

Simon.
Shay (10) [Avatar] Offline
#4
Re: How to remove Parentheses "()" from a string
Hi Simon

You can get more built-in help by typing at the console:

help about_regular_expression
help about_escape_character

-----
Shay Levi
$cript Fanatic
http://scriptolog.blogspot.com