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

Simple Regular Expression

Closed Thread
 
Thread Tools Display Modes
Old 01-30-2008   #1 (permalink)
BJ
Guest


 

Simple Regular Expression

Hello Group,

This string matches although the number has 5 digits.
What's wrong?


$string = "NYC-CC-35225"
$expression = "[a-z]{3}\-[a-z]{2}\-[0-9]{4}"

$string -match $expression
Old 01-30-2008   #2 (permalink)
Tom Moreau
Guest


 

Re: Simple Regular Expression

Technically, it is correct. Looking just at the last bit, you asked it to
match 4 digits, and there were 4 digits. There was also another digit, but
that was not part of the match. If you want the entire string to match from
beginning to end, try:

$expression = "^[a-z]{3}\-[a-z]{2}\-[0-9]{4}$"

--
Tom

----------------------------------------------------
Thomas A. Moreau, BSc, PhD, MCSE, MCDBA, MCITP, MCTS
SQL Server MVP
Toronto, ON Canada
https://mvp.support.microsoft.com/profile/Tom.Moreau


"BJ" <post@xxxxxx> wrote in message
news:1b523a23-05d3-4dc0-9285-9a4f7ad00eb7@xxxxxx
Hello Group,

This string matches although the number has 5 digits.
What's wrong?


$string = "NYC-CC-35225"
$expression = "[a-z]{3}\-[a-z]{2}\-[0-9]{4}"

$string -match $expression


Old 01-30-2008   #3 (permalink)
BJ
Guest


 

Re: Simple Regular Expression

Thank you very much, it works!
It will take a long time to get used to regular expressions,
especially for a newbie like myself.......

On 30 Jan., 20:32, "Tom Moreau" <t...@xxxxxx> wrote:
Quote:

> Technically, it is correct. * Looking just at the last bit, you asked itto
> match 4 digits, and there were 4 digits. *There was also another digit, but
> that was not part of the match. *If you want the entire string to match from
> beginning to end, try:
>
> $expression = "^[a-z]{3}\-[a-z]{2}\-[0-9]{4}$"
>
> --
> * *Tom
>
> ----------------------------------------------------
> Thomas A. Moreau, BSc, PhD, MCSE, MCDBA, MCITP, MCTS
> SQL Server MVP
> Toronto, ON * Canadahttps://mvp.support.microsoft.com/profile/Tom.Moreau
>
> "BJ" <p...@xxxxxx> wrote in message
>
> news:1b523a23-05d3-4dc0-9285-9a4f7ad00eb7@xxxxxx
> Hello Group,
>
> This string matches although the number has 5 digits.
> What's wrong?
>
> $string = "NYC-CC-35225"
> $expression = "[a-z]{3}\-[a-z]{2}\-[0-9]{4}"
>
> $string -match $expression
Old 01-30-2008   #4 (permalink)
Tom Moreau
Guest


 

Re: Simple Regular Expression

The problem for me is that I am a Perl jock and the syntax for Perl vs. MS
regular expressions is somewhat different. I was looking for negative and
positive look-ahead and look-behind and couldn't come up with what I wanted.

--
Tom

----------------------------------------------------
Thomas A. Moreau, BSc, PhD, MCSE, MCDBA, MCITP, MCTS
SQL Server MVP
Toronto, ON Canada
https://mvp.support.microsoft.com/profile/Tom.Moreau


"BJ" <post@xxxxxx> wrote in message
news:026e669a-dc07-4fa1-b586-7a99bcff4944@xxxxxx
Thank you very much, it works!
It will take a long time to get used to regular expressions,
especially for a newbie like myself.......

On 30 Jan., 20:32, "Tom Moreau" <t...@xxxxxx> wrote:
Quote:

> Technically, it is correct. Looking just at the last bit, you asked it to
> match 4 digits, and there were 4 digits. There was also another digit, but
> that was not part of the match. If you want the entire string to match
from
Quote:

> beginning to end, try:
>
> $expression = "^[a-z]{3}\-[a-z]{2}\-[0-9]{4}$"
>
> --
> Tom
>
> ----------------------------------------------------
> Thomas A. Moreau, BSc, PhD, MCSE, MCDBA, MCITP, MCTS
> SQL Server MVP
> Toronto, ON Canadahttps://mvp.support.microsoft.com/profile/Tom.Moreau
>
> "BJ" <p...@xxxxxx> wrote in message
>
> news:1b523a23-05d3-4dc0-9285-9a4f7ad00eb7@xxxxxx
> Hello Group,
>
> This string matches although the number has 5 digits.
> What's wrong?
>
> $string = "NYC-CC-35225"
> $expression = "[a-z]{3}\-[a-z]{2}\-[0-9]{4}"
>
> $string -match $expression

Old 01-30-2008   #5 (permalink)
Shay Levi
Guest


 

Re: Simple Regular Expression


Here's the .NET syntax

(?= ...) Positive lookahead
(?! ...) Negative lookahead
(?<= ...) Positive lookbehind
(?<! ...) Negative lookbehind


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

> The problem for me is that I am a Perl jock and the syntax for Perl
> vs. MS regular expressions is somewhat different. I was looking for
> negative and positive look-ahead and look-behind and couldn't come up
> with what I wanted.
>
> ----------------------------------------------------
> Thomas A. Moreau, BSc, PhD, MCSE, MCDBA, MCITP, MCTS
> SQL Server MVP
> Toronto, ON Canada
> https://mvp.support.microsoft.com/profile/Tom.Moreau
> "BJ" <post@xxxxxx> wrote in message
> news:026e669a-dc07-4fa1-b586-7a99bcff4944@xxxxxx
> ...
> Thank you very much, it works!
> It will take a long time to get used to regular expressions,
> especially for a newbie like myself.......
> On 30 Jan., 20:32, "Tom Moreau" <t...@xxxxxx> wrote:
>
Quote:

>> Technically, it is correct. Looking just at the last bit, you asked
>> it to match 4 digits, and there were 4 digits. There was also another
>> digit, but that was not part of the match. If you want the entire
>> string to match
>>
> from
>
Quote:

>> beginning to end, try:
>>
>> $expression = "^[a-z]{3}\-[a-z]{2}\-[0-9]{4}$"
>>
>> --
>> Tom
>> ----------------------------------------------------
>> Thomas A. Moreau, BSc, PhD, MCSE, MCDBA, MCITP, MCTS
>> SQL Server MVP
>> Toronto, ON
>> Canadahttps://mvp.support.microsoft.com/profile/Tom.Moreau
>> "BJ" <p...@xxxxxx> wrote in message
>>
>> news:1b523a23-05d3-4dc0-9285-9a4f7ad00eb7@xxxxxx
>> ... Hello Group,
>>
>> This string matches although the number has 5 digits. What's wrong?
>>
>> $string = "NYC-CC-35225"
>> $expression = "[a-z]{3}\-[a-z]{2}\-[0-9]{4}"
>> $string -match $expression
>>

Old 01-30-2008   #6 (permalink)
Tom Moreau
Guest


 

Re: Simple Regular Expression

Thanx. Looks like it's the same as Perl. Have you got a link to this at
the MSDN site?

--
Tom

----------------------------------------------------
Thomas A. Moreau, BSc, PhD, MCSE, MCDBA, MCITP, MCTS
SQL Server MVP
Toronto, ON Canada
https://mvp.support.microsoft.com/profile/Tom.Moreau


"Shay Levi" <no@xxxxxx> wrote in message
news:8766a9441c84f8ca31a6cfa302f8@xxxxxx

Here's the .NET syntax

(?= ...) Positive lookahead
(?! ...) Negative lookahead
(?<= ...) Positive lookbehind
(?<! ...) Negative lookbehind


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

> The problem for me is that I am a Perl jock and the syntax for Perl
> vs. MS regular expressions is somewhat different. I was looking for
> negative and positive look-ahead and look-behind and couldn't come up
> with what I wanted.
>
> ----------------------------------------------------
> Thomas A. Moreau, BSc, PhD, MCSE, MCDBA, MCITP, MCTS
> SQL Server MVP
> Toronto, ON Canada
> https://mvp.support.microsoft.com/profile/Tom.Moreau
> "BJ" <post@xxxxxx> wrote in message
> news:026e669a-dc07-4fa1-b586-7a99bcff4944@xxxxxx
> ...
> Thank you very much, it works!
> It will take a long time to get used to regular expressions,
> especially for a newbie like myself.......
> On 30 Jan., 20:32, "Tom Moreau" <t...@xxxxxx> wrote:
>
Quote:

>> Technically, it is correct. Looking just at the last bit, you asked
>> it to match 4 digits, and there were 4 digits. There was also another
>> digit, but that was not part of the match. If you want the entire
>> string to match
>>
> from
>
Quote:

>> beginning to end, try:
>>
>> $expression = "^[a-z]{3}\-[a-z]{2}\-[0-9]{4}$"
>>
>> --
>> Tom
>> ----------------------------------------------------
>> Thomas A. Moreau, BSc, PhD, MCSE, MCDBA, MCITP, MCTS
>> SQL Server MVP
>> Toronto, ON
>> Canadahttps://mvp.support.microsoft.com/profile/Tom.Moreau
>> "BJ" <p...@xxxxxx> wrote in message
>>
>> news:1b523a23-05d3-4dc0-9285-9a4f7ad00eb7@xxxxxx
>> ... Hello Group,
>>
>> This string matches although the number has 5 digits. What's wrong?
>>
>> $string = "NYC-CC-35225"
>> $expression = "[a-z]{3}\-[a-z]{2}\-[0-9]{4}"
>> $string -match $expression
>>


Old 01-30-2008   #7 (permalink)
Shay Levi
Guest


 

Re: Simple Regular Expression

http://msdn2.microsoft.com/en-us/library/ms972966.aspx

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

> Thanx. Looks like it's the same as Perl. Have you got a link to this
> at the MSDN site?
>
> ----------------------------------------------------
> Thomas A. Moreau, BSc, PhD, MCSE, MCDBA, MCITP, MCTS
> SQL Server MVP
> Toronto, ON Canada
> https://mvp.support.microsoft.com/profile/Tom.Moreau
> "Shay Levi" <no@xxxxxx> wrote in message
> news:8766a9441c84f8ca31a6cfa302f8@xxxxxx
> Here's the .NET syntax
>
> (?= ...) Positive lookahead
> (?! ...) Negative lookahead
> (?<= ...) Positive lookbehind
> (?<! ...) Negative lookbehind
> -----
> Shay Levi
> $cript Fanatic
> http://scriptolog.blogspot.com
Quote:

>> The problem for me is that I am a Perl jock and the syntax for Perl
>> vs. MS regular expressions is somewhat different. I was looking for
>> negative and positive look-ahead and look-behind and couldn't come up
>> with what I wanted.
>>
>> ----------------------------------------------------
>> Thomas A. Moreau, BSc, PhD, MCSE, MCDBA, MCITP, MCTS
>> SQL Server MVP
>> Toronto, ON Canada
>> https://mvp.support.microsoft.com/profile/Tom.Moreau
>> "BJ" <post@xxxxxx> wrote in message
>> news:026e669a-dc07-4fa1-b586-7a99bcff4944@xxxxxx
>> m
>> ...
>> Thank you very much, it works!
>> It will take a long time to get used to regular expressions,
>> especially for a newbie like myself.......
>> On 30 Jan., 20:32, "Tom Moreau" <t...@xxxxxx> wrote:
Quote:

>>> Technically, it is correct. Looking just at the last bit, you asked
>>> it to match 4 digits, and there were 4 digits. There was also
>>> another digit, but that was not part of the match. If you want the
>>> entire string to match
>>>
>> from
>>
Quote:

>>> beginning to end, try:
>>>
>>> $expression = "^[a-z]{3}\-[a-z]{2}\-[0-9]{4}$"
>>>
>>> --
>>> Tom
>>> ----------------------------------------------------
>>> Thomas A. Moreau, BSc, PhD, MCSE, MCDBA, MCITP, MCTS
>>> SQL Server MVP
>>> Toronto, ON
>>> Canadahttps://mvp.support.microsoft.com/profile/Tom.Moreau
>>> "BJ" <p...@xxxxxx> wrote in message
>>> news:1b523a23-05d3-4dc0-9285-9a4f7ad00eb7@xxxxxx
>>> m ... Hello Group,
>>>
>>> This string matches although the number has 5 digits. What's wrong?
>>>
>>> $string = "NYC-CC-35225"
>>> $expression = "[a-z]{3}\-[a-z]{2}\-[0-9]{4}"
>>> $string -match $expression

Closed Thread

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
regular expression help Stimp VB Script 9 08-02-2008 06:05 PM
Results from Regular Expression Match Mike PowerShell 2 05-20-2008 08:26 PM
Re: regular expression howto Keith Hill [MVP] PowerShell 0 01-08-2008 09:45 PM
Regular expression notes John Cook PowerShell 2 07-13-2007 04:10 PM
simple regular expression Ben PowerShell 2 02-19-2007 08:28 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 50