Windows Vista Forums
Vista Forums Home Join Vista Forums Donate Vista Tutorials Tags

Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
Register at Vista forums...the world biggest Windows Vista resource Join Vista Forums Now

Go Back   Vista Forums > Microsoft Technical Newsgroups > PowerShell

Select-String pattern quirk???

Closed Thread
 
Thread Tools Display Modes
Old 01-19-2007   #1 (permalink)
$hay
Guest
 
Posts: n/a

Select-String pattern quirk???

the -pattern in select string specifies the string or regular expression
that represents the matching criteria.

when i try to search for "$hay":

"$hay bla bla" | select-string -pattern "$hay"

it returns an error

Select-String : Cannot bind argument to parameter 'Pattern' because it is an
empty string.
At line:1 char:34
+ "$hay bla bla" | select-string <<<< "$hay"

when escaping the $ sign:

"$hay bla bla" | select-string -pattern "\$hay"

it returns an error

Select-String : parsing "\" - Illegal \ at end of pattern.
At line:1 char:34
+ "$hay bla bla" | select-string <<<< "\$hay"

just to be sure on regex pattern, i tried

"$hay bla bla" -match "$hay"

and it returns true without escaping the $ sign.

i guess the $ sign is the trouble maker. any suggestions?

--
$hay
http://scriptolog.blogspot.com



 
Old 01-19-2007   #2 (permalink)
Keith Hill [MVP]
Guest
 
Posts: n/a

Re: Select-String pattern quirk???

"$hay" <no@addre.ss> wrote in message
news:eff9Gm9OHHA.5000@TK2MSFTNGP03.phx.gbl...
> "$hay bla bla" -match "$hay"
>
> and it returns true without escaping the $ sign.
>
> i guess the $ sign is the trouble maker. any suggestions?


Yep. Try this:

'$hay bla bla' | select-string -pattern '\$hay'

The single quotes turn off PoSh's variable expansion. Then you need to
escape the $ in the regex because that is the end-of-line marker.

--
Keith


 
Old 01-19-2007   #3 (permalink)
/\/\o\/\/ [MVP]
Guest
 
Posts: n/a

RE: Select-String pattern quirk???

Note that you have 2 ways of escaping here (PowerShell / backtick) and regex :
you can use single quotes or escape them (powerShell backtick )

examples :

PS H:\> '$hay bla bla' | select-string -pattern '$hay'
PS H:\> '$hay bla bla' | select-string -pattern '\$hay'

$hay bla bla


PS H:\> "`$hay bla bla" | select-string -pattern '\$hay'

$hay bla bla


PS H:\> "`$hay bla bla" | select-string -pattern "\`$hay"

$hay bla bla




"$hay" wrote:

> the -pattern in select string specifies the string or regular expression
> that represents the matching criteria.
>
> when i try to search for "$hay":
>
> "$hay bla bla" | select-string -pattern "$hay"
>
> it returns an error
>
> Select-String : Cannot bind argument to parameter 'Pattern' because it is an
> empty string.
> At line:1 char:34
> + "$hay bla bla" | select-string <<<< "$hay"
>
> when escaping the $ sign:
>
> "$hay bla bla" | select-string -pattern "\$hay"
>
> it returns an error
>
> Select-String : parsing "\" - Illegal \ at end of pattern.
> At line:1 char:34
> + "$hay bla bla" | select-string <<<< "\$hay"
>
> just to be sure on regex pattern, i tried
>
> "$hay bla bla" -match "$hay"
>
> and it returns true without escaping the $ sign.
>
> i guess the $ sign is the trouble maker. any suggestions?
>
> --
> $hay
> http://scriptolog.blogspot.com
>
>
>
>

 
Old 01-19-2007   #4 (permalink)
$hay
Guest
 
Posts: n/a

Re: Select-String pattern quirk???

Thanks Keith and Mow, this resolves everything

$hay
http://scriptolog.blogspot.com


"$hay" <no@addre.ss> wrote in message
news:eff9Gm9OHHA.5000@TK2MSFTNGP03.phx.gbl...
> the -pattern in select string specifies the string or regular expression
> that represents the matching criteria.
>
> when i try to search for "$hay":
>
> "$hay bla bla" | select-string -pattern "$hay"
>
> it returns an error
>
> Select-String : Cannot bind argument to parameter 'Pattern' because it is
> an empty string.
> At line:1 char:34
> + "$hay bla bla" | select-string <<<< "$hay"
>
> when escaping the $ sign:
>
> "$hay bla bla" | select-string -pattern "\$hay"
>
> it returns an error
>
> Select-String : parsing "\" - Illegal \ at end of pattern.
> At line:1 char:34
> + "$hay bla bla" | select-string <<<< "\$hay"
>
> just to be sure on regex pattern, i tried
>
> "$hay bla bla" -match "$hay"
>
> and it returns true without escaping the $ sign.
>
> i guess the $ sign is the trouble maker. any suggestions?
>
> --
> $hay
> http://scriptolog.blogspot.com
>
>
>



 
 
Closed Thread

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
problems with $var | select-string -pattern $string -q Ben Christian PowerShell 3 02-08-2008 12:41 PM
Select-String problem Keith Hill [MVP] PowerShell 3 06-17-2007 12:18 PM
Re: select-string exceptions cmyers PowerShell 0 05-23-2007 02:09 PM
Re: select-string exceptions cmyers PowerShell 0 05-23-2007 02:09 PM
select-string to act like grep -v Frank PowerShell 5 05-12-2007 12:59 AM








Vistax64.com 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 2005-2008

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 47 48 49