Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - Simple Regular Expression

Reply
 
Old 01-30-2008   #1 (permalink)
BJ


 
 

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

My System SpecsSystem Spec
Old 01-30-2008   #2 (permalink)
Tom Moreau


 
 

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


My System SpecsSystem Spec
Old 01-30-2008   #3 (permalink)
BJ


 
 

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
My System SpecsSystem Spec
Old 01-30-2008   #4 (permalink)
Tom Moreau


 
 

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

My System SpecsSystem Spec
Old 01-30-2008   #5 (permalink)
Shay Levi


 
 

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
>>

My System SpecsSystem Spec
Old 01-30-2008   #6 (permalink)
Tom Moreau


 
 

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
>>


My System SpecsSystem Spec
Old 01-30-2008   #7 (permalink)
Shay Levi


 
 

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

My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Regular Expression help C# .NET General
Regular Expression for ../ .NET General
Help with a regular expression VB Script
regular expression help VB Script
simple regular expression PowerShell


Vista Forums 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 Ltd

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