header
header Register : : Login header
header
divider
menuleft
menuright
submenu
left

[August 25th, 2008] Check the home page regarding PowerShell related news from a brand new sponsor: Idera

error when using -match operator
Last Post 13 Aug 2008 03:01 AM by glnsize. 7 Replies.
Printer Friendly
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages
tHyEstesUser is Offline
New Member
New Member
Posts:13

--
03 Jan 2008 03:18 PM  

Hello,

 

Why is it that when is use the match operator I get an error?

 

I do the following:

[string] $test = "C:\users\Default"

If ($test -match "C:\users";) {write-host "OK"}

The '-match' operator failed: parsing "c:\users" - Insufficient hexadecimal digits..
At line:1 char:17
+ if ($test -match  <<<< "c:\users";) {write-host "oke"}

Does anyone know how solve this?

Thanks,

Martijn

bsonposhUser is Offline
Basic Member
Basic Member
Posts:388

--
03 Jan 2008 03:23 PM  
-match uses regex so you have to escape "\"

if($test -match "c:\\users"){write-host "oke"}
SAPIENScripterUser is Offline
New Member
New Member
Posts:45

--
03 Jan 2008 03:27 PM  
-match is doing a regular expression match and the \ character is used in regular expressions. You need to escape it like this:

If ($test -match "C:\\users") {write-host "OK"}
PoshoholicUser is Offline
New Member
New Member
Posts:29

--
05 Jan 2008 03:14 AM  

Just in case you were wondering, \u is used to identify a Unicode character in a regular expression string.  When using \u, you must follow it with 4-characters that will make up a hexadecimal value identifying which Unicode character to use.  In your case, \u was followed by s, and s is not one of the characters used to identify hexadecimal values (0..9 and a..f), so the PowerShell parser throws a warning correctly indicating that there were insufficient hexadecimal digits following the \u character.

Fortunately your path wasn't something like C:\ufeed\mydog, because then you might have tried to match this:

"C:\ufeed\mydog" -match "C:\ufeed"

This might appear like it should return true to the untrained eye however it will correctly return false.  The "C:\ufeed" string actually evaluates to:

ﻭ:C""

which of course is not a valid match for "C:\ufeed\mydog".

:)

Of course, all of this is moot because you already found out from Brandon and Jeff that you need to escape your backslash character on the right-hand side of match with a second backslash.

--
Kirk Munro [MVP]
Poshoholic
http://poshoholic.com

tHyEstesUser is Offline
New Member
New Member
Posts:13

--
07 Jan 2008 09:13 AM  

Hi All,

 

Thanks for clearly explaining this.

 

Regards

Martijn

General_DarkUser is Offline
New Member
New Member
Posts:2

--
12 Aug 2008 08:10 AM  
I'm having the same problem, but I cant type in what to match manually since I'm getting the string to match against from an array from a remote hosts eventlog.
Any ideas?
ShayUser is Offline
Basic Member
Basic Member
Posts:228

--
12 Aug 2008 08:20 AM  

 

You can use the Regex escape() static method when you don't know what metacharacters your value might contain. Basically, it escapes a minimal set of metacharacters (\, *, +, ?, |, {, [, (,), ^, $,., #, and white space) by replacing them with their escape codes. 

PS > [regex]::escape("c:\users")
c:\\users

glnsizeUser is Online
New Member
New Member
Posts:84

--
13 Aug 2008 03:01 AM  

One, Two, Three, FOUR MVP'S!  That's awsome,  yet another reason powershell rules them all!  Even if it does appear that JH stole BS answer :)

~Glenn

 

You are not authorized to post a reply.

Active Forums 4.1
right
   
footer Sponsored by Quest Software • SAPIEN Technologies • ShellTools, LLC • Microsoft Windows Server 2008 footer
footer