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

Vista - Help with a regular expression

Reply
 
Old 09-30-2008   #1 (permalink)
PO


 
 

Help with a regular expression

Hi

Can someone help me with a regular expression to validate the following
string:

1. A number must be between 1000 and 9999
2. Several numbers can be entered in a sequence separates with a comma
3. Not more then 5 numbers can be entered at one time


Example of valid entries:
=================
1052
1052,1175,1951
1052,1175,1951,1000,1157

Example of invalid entries
==================:
0995 (must be between 1000 and 9999)
1052,12, 1175 (must be between 1000 and 9999, and no spaces)
1052;1175;1951; (comma must be used to separate numbers and no trailing
commas)
1052,1175,1951,1000,1157,7852 (no more then 5 numbers may be entered)


Thanks in advance
Pete



My System SpecsSystem Spec
Old 09-30-2008   #2 (permalink)
Steve


 
 

Re: Help with a regular expression

PO wrote:
Quote:

>
> Can someone help me with a regular expression to validate the
> following string:
>
> 1. A number must be between 1000 and 9999
> 2. Several numbers can be entered in a sequence separates with a comma
> 3. Not more then 5 numbers can be entered at one time
>
^[1-9]\d{3}(?:,[1-9]\d{3}){0,4}$

---

test = array("1052", _
"1052,1175,1951", _
"1052,1175,1951,1000,1157", _
"0995", _
"1052,12, 1175", _
"1052;1175;1951;", _
"1052,1175,1951,1000,1157,7852" _
)

with new regexp
.pattern = "^[1-9]\d{3}(?:,[1-9]\d{3}){0,4}$"
for each str in test
wscript.echo .test(str) & vbTab & str
next
end with

--
Steve

The belief in the possibility of a short decisive war appears to be one
of the most ancient and dangerous of human illusions. -Robert Lynd


My System SpecsSystem Spec
Old 09-30-2008   #3 (permalink)
PO


 
 

Re: Help with a regular expression

Thanks a lot Steve. Works just like I want.
If I want to increase or decrease the number of values a user can enter, do
I just change the last number i.e. ^[1-9]\d{3}(?:,[1-9]\d{3}){0,5}$ will
allow 6 values?

Regards
Pete

"Steve" <cerberus40@xxxxxx> skrev i meddelandet
news:uE0aL3uIJHA.1088@xxxxxx
Quote:

> PO wrote:
Quote:

>>
>> Can someone help me with a regular expression to validate the
>> following string:
>>
>> 1. A number must be between 1000 and 9999
>> 2. Several numbers can be entered in a sequence separates with a comma
>> 3. Not more then 5 numbers can be entered at one time
>>
>
> ^[1-9]\d{3}(?:,[1-9]\d{3}){0,4}$
>
> ---
>
> test = array("1052", _
> "1052,1175,1951", _
> "1052,1175,1951,1000,1157", _
> "0995", _
> "1052,12, 1175", _
> "1052;1175;1951;", _
> "1052,1175,1951,1000,1157,7852" _
> )
>
> with new regexp
> .pattern = "^[1-9]\d{3}(?:,[1-9]\d{3}){0,4}$"
> for each str in test
> wscript.echo .test(str) & vbTab & str
> next
> end with
>
> --
> Steve
>
> The belief in the possibility of a short decisive war appears to be one
> of the most ancient and dangerous of human illusions. -Robert Lynd
>

My System SpecsSystem Spec
Old 09-30-2008   #4 (permalink)
Paul Randall


 
 

Re: Help with a regular expression

The first thing you need to find out before modifying someone's regular
expression is to analyze what it does and how it works. A useful but
imperfect tool for this is Regular Expression Workbench, a link to which you
should be able to find by groups.googling this
microsoft.public.scripting.vbscript newsgroup. Note that Regular Expression
Workbench uses the dot net regular expression engine rather than the
VBScript regular expression engine, so it is not perfect for VBScripting
purposes and will sometimes lead you astray. It is often helpful, as in
this case.

The regular expression ^[1-9]\d{3}(?:,[1-9]\d{3}){0,4}$
is interpreted to mean:
^ (anchor to start of string)
Any character in "1-9"
Any digit
Exactly 3 times
Non-capturing Group
,
Any character in "1-9"
Any digit
Exactly 3 times
End Capture
At least 0, but not more than 4 times
$ (anchor to end of string)

I'm not sure whether this interpretation correctly handles the ?: in the
regular expression -- I don't know what those two characters do here.

Assuming that this dot net interpretation of the original regular expression
actually is the author's intent, then is is pretty easy to see that the
current pattern requires four digits per number, the first digit of any
number cannot be a zero, and a comma preceeds the four digits for all
numbers following the first number, with zero to 5 numbers on a line.

With Regular Expression Workbench you can easily modify the regular
expression and execute it on a test string.

-Paul Randall

"PO" <h> wrote in message news:uQr2spwIJHA.2156@xxxxxx
Quote:

> Thanks a lot Steve. Works just like I want.
> If I want to increase or decrease the number of values a user can enter,
> do I just change the last number i.e. ^[1-9]\d{3}(?:,[1-9]\d{3}){0,5}$
> will allow 6 values?
>
> Regards
> Pete
>
> "Steve" <cerberus40@xxxxxx> skrev i meddelandet
> news:uE0aL3uIJHA.1088@xxxxxx
Quote:

>> PO wrote:
Quote:

>>>
>>> Can someone help me with a regular expression to validate the
>>> following string:
>>>
>>> 1. A number must be between 1000 and 9999
>>> 2. Several numbers can be entered in a sequence separates with a comma
>>> 3. Not more then 5 numbers can be entered at one time
>>>
>>
>> ^[1-9]\d{3}(?:,[1-9]\d{3}){0,4}$
>>
>> ---
>>
>> test = array("1052", _
>> "1052,1175,1951", _
>> "1052,1175,1951,1000,1157", _
>> "0995", _
>> "1052,12, 1175", _
>> "1052;1175;1951;", _
>> "1052,1175,1951,1000,1157,7852" _
>> )
>>
>> with new regexp
>> .pattern = "^[1-9]\d{3}(?:,[1-9]\d{3}){0,4}$"
>> for each str in test
>> wscript.echo .test(str) & vbTab & str
>> next
>> end with
>>
>> --
>> Steve
>>
>> The belief in the possibility of a short decisive war appears to be one
>> of the most ancient and dangerous of human illusions. -Robert Lynd
>>
>
>

My System SpecsSystem Spec
Old 10-01-2008   #5 (permalink)
Steve


 
 

Re: Help with a regular expression

Paul Randall wrote:
Quote:

>
> I'm not sure whether this interpretation correctly handles the ?: in
> the regular expression -- I don't know what those two characters do
> here.
They stop the parentheses from creating a submatch when the regular
expression is executed. Any time you use parentheses in a pattern and
don't use the Submatches collection (or don't use all the members of the
collection) you should use (?: ) to group any submatch you're not
interested in.

I'm not sure about the .NET regexp engine, but the VBScript engine does
not degrade well for complex patterns. When a pattern crosses the
tipping point, the engine "hits the wall" and slows to a crawl. Anything
you can do to cut down the work the engine needs to do and moves the
wall a little further back.

I don't know if the VBScript regexp's Test method is optimized, but I
suspect that it just calls the same code as the Execute method and tests
the Count property of the returned Matches collection.

And in this case, a submatch is not very useful--it would only capture
the last group of comma and digits and ignore any preceeding ones.

--
Steve

We have just enough religion to make us hate but not enough to make us
love one another. -Jonathan Swift


My System SpecsSystem Spec
Old 10-01-2008   #6 (permalink)
Steve


 
 

Re: Help with a regular expression

PO wrote:
Quote:

> Thanks a lot Steve. Works just like I want.
> If I want to increase or decrease the number of values a user can
> enter, do I just change the last number i.e.
> ^[1-9]\d{3}(?:,[1-9]\d{3}){0,5}$ will allow 6 values?
>
Yes. And you can change the minimum number of values too.
^[1-9]\d{3}(?:,[1-9]\d{3}){1,4}$ will only accept 2-5 values.

Paul's advice is good--you should analyze and understand what a pattern
does before you use it. I know regexp patterns seem like arcane
incatations, as though you should be sacrificing a chicken while
reciting them during a new moon, but if you keep working with them
you'll reach an "aha moment" and suddenly they start to make sense. For
a long time I didn't use regexps because I didn't understand them and I
didn't understand them because I didn't use them. ;-) Once you get the
hang of them, you'll find them incredibly useful and wonder how you ever
got along without them.

--
Steve

The world is a tragedy to those who feel, but a comedy to those who
think. -Horace Walpole


My System SpecsSystem Spec
Old 10-01-2008   #7 (permalink)
James Whitlow


 
 

Re: Help with a regular expression

"Steve" <cerberus40@xxxxxx> wrote in message
news:uhduVL7IJHA.1556@xxxxxx
Quote:

> PO wrote:
Quote:

> For a long time I didn't use regexps because I didn't understand them and
> I didn't understand them because I didn't use them. ;-) Once you get the
> hang of them, you'll find them incredibly useful and wonder how you ever
> got along without them.
I am in complete agreement with you! Like you, I avoided them for a long
time, thinking they had a high learning curve with little payoff. I use at
least one regular expression in the majority of the scripts I write now. I
really wish Microsoft had included 'lookbehind', though. I have only needed
it a few times, but in those instances there was no good work-around. I am
glad that we did get 'lookahead', though.

These are my three most often used syntax pages:

http://msdn.microsoft.com/en-us/library/ms974570.aspx
http://msdn.microsoft.com/en-us/libr...41(VS.71).aspx
http://regexlib.com/CheatSheet.aspx


My System SpecsSystem Spec
Old 10-01-2008   #8 (permalink)
Steve


 
 

Re: Help with a regular expression

James Whitlow wrote:
Quote:

> "Steve" <cerberus40@xxxxxx> wrote in message
> news:uhduVL7IJHA.1556@xxxxxx
>
Quote:

>> For a long time I didn't use regexps because I didn't understand
>> them and I didn't understand them because I didn't use them. ;-)
>> Once you get the hang of them, you'll find them incredibly useful
>> and wonder how you ever got along without them.
>
> I am in complete agreement with you! Like you, I avoided them for
> a long time, thinking they had a high learning curve with little
> payoff. I use at least one regular expression in the majority of the
> scripts I write now. I really wish Microsoft had included
> 'lookbehind', though. I have only needed it a few times, but in those
> instances there was no good work-around. I am glad that we did get
> 'lookahead', though.
:Lookbehind would be useful (and is included in .NET regexps), but I
really wish MS had included the Singleline mode for VBScript regexps
(also in .NET). The workaround is pretty straightforward but it's so
much simpler to code "." than "[\s\S]" or "[\w\W]" when you want to
match every character including newlines.
All useful. I also go to
http://msdn.microsoft.com/en-us/libr...pting12_topic4
when I can't remember how to code replacement function in VBScript.

--
Steve

If someone does something we disapprove of, we regard him as bad if we
believe we can deter him from persisting in his conduct, but we regard
him as mad if we believe we cannot. -Thomas Szasz



My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Regular Expression help C# .NET General
regular expression capture PowerShell
Regular Expression for ../ .NET General
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