comparing a string that contains a parenthesis

C

Caliman

Hi, I'm having trouble finding documentation on this.
For example, if a string value is "Intel(R) Pentium(R) 4 CPU 2.00GHz",
using -match " 4 CPU 2.00" gives True, but
using -match ") 4 CPU 2.00" gives False, and
using -match "`) 4 CPU 2.00" gives False, and
using -match ') 4 CPU 2.00' gives False.

How do you compare a string if it contains a parenthesis?
Thx.
 

My Computer

K

Kiron

-match uses regular expression. RegEx special char must be escaped with a
backward slash '\'.

$str = 'Intel(R) Pentium(R) 4 CPU 2.00GHz'
$str -match '\) 4 CPU 2.00'

# these are the RegEx special chars that need to scape
# . $ ^ { [ ( ) * + ? \ |

--
Kiron
 

My Computer

C

Caliman

Thanks, Kiron. That works for me. So, when do I use the backtick character
'`' to escape a character?

"Kiron" wrote:

> -match uses regular expression. RegEx special char must be escaped with a
> backward slash '\'.
>
> $str = 'Intel(R) Pentium(R) 4 CPU 2.00GHz'
> $str -match '\) 4 CPU 2.00'
>
> # these are the RegEx special chars that need to scape
> # . $ ^ { [ ( ) * + ? \ |
>
> --
> Kiron
 

My Computer

K

Kiron

The backtick '`' is used to escape PowerShell special chareacters in
double-quoted strings. It's also used to prevent a variable from expanding in
a doubke-quoted string. Also, the backtick is used to indicate line
continuation

You can read about this:

get-help about_escape_character
get-help about_special_characters

--
Kiron
 

My Computer

S

Shay Levy [MVP]

Hi Caliman,


You can also use the escape() static method to do the magic for you, it is
very usefull when you dont know the string content in advance (like with
variables content):

PS > [regex]::escape(") 4 CPU 2.00")
\)\ 4\ CPU\ 2\.00



PS > "Intel(R) Pentium(R) 4 CPU 2.00GHz" -match [regex]::escape(") 4 CPU
2.00")
True



---
Shay Levy
Windows PowerShell MVP
http://blogs.microsoft.co.il/blogs/ScriptFanatic
PowerShell Toolbar: http://tinyurl.com/PSToolbar



C> Thanks, Kiron. That works for me. So, when do I use the backtick
C> character '`' to escape a character?
C>
C> "Kiron" wrote:
C>

>> -match uses regular expression. RegEx special char must be escaped
>> with a backward slash '\'.
>>
>> $str = 'Intel(R) Pentium(R) 4 CPU 2.00GHz'
>> $str -match '\) 4 CPU 2.00'
>> # these are the RegEx special chars that need to scape # . $ ^ { [ (
>> ) * + ? \ |
>>
>> -- Kiron
>>
 

My Computer

J

Josh Einstein

Sounds like what you want in this case is the -like operator.

'Hello World' -like 'Hello *'
True

Josh Einstein

"Caliman" <[email protected]> wrote in message
news:[email protected]

> Hi, I'm having trouble finding documentation on this.
> For example, if a string value is "Intel(R) Pentium(R) 4 CPU 2.00GHz",
> using -match " 4 CPU 2.00" gives True, but
> using -match ") 4 CPU 2.00" gives False, and
> using -match "`) 4 CPU 2.00" gives False, and
> using -match ') 4 CPU 2.00' gives False.
>
> How do you compare a string if it contains a parenthesis?
> Thx.
 

My Computer

Top