Re: Help with RegExp by ekkehard
ekkehard
Mon Sep 03 09:17:19 PDT 2007
PO schrieb:
> I need help with the following 3 regular expressions:
>
> 1. Using the regexp pattern "^(?:[AUT]\d{5}(?:,|$))+$" the following string
> is valid:
> A12345,A23456,U45678,U12375,T12345,U87456. How would I have to modify the
> pattern to allow for a maximum of 5 values e.g.
> A12345,A23456,U45678,U12375,T12345?
>
>
> 2. Which pattern do I have to use to validate the following string, e.g.:
> 1001,5458,3112
> Valid numbers are between 1000 and 9999. Each number needs to be separated
> with a comma. The string can only contain a maximum of 10 numbers.
>
>
> 3. Which pattern do I have to use to validate the following string, e.g.:
> 2007
> Valid numbers are between 2000 and 2020. Only 1 number is allowed.
>
> TIA
> Pete
>
>
Some code to make developing/testing RegExps for your 3 problems a bit easier:
aRVal = Array( 0, "done" )
Dim aTests : aTests = Array( _
Array( New RegExp, Array( _
"A12345,A23456,U45678,U12375,T12345,U87456" _
, "A12345,A23456,U45678,U12375,T12345" _
, "A12345,A23456,U45678,U12375" _
, "A12345,A23456,U45678" _
, "A12345,A23456" _
, "A12345" _
, "A1234" _
, "" _
, "A123456,A23456,U45678,U12375,T12345,U87456" _
) _
) _
, Array( New RegExp, Array( _
"1001,5458,3112" _
, "1001,5458,3112,5458,3112,5458,3112,5458,3112,5458,3112" _
, "1001,5458,3112,5458,3112,5458,3112,5458,3112,5458" _
) _
) _
, Array( New RegExp, Array( _
"1999" _
, "2000", "2001", "2002", "2003", "2004" _
, "2005", "2006", "2007", "2008", "2009" _
, "2010", "2011", "2012", "2013", "2014" _
, "2015", "2016", "2017", "2018", "2019" _
, "2020" _
, "2021" _
, "1907", "ABCD" _
, "22007", "20201" _
) _
) _
)
' to allow for a maximum of 5 values e.g. A12345,A23456,U45678,U12375,T12345
aTests( 0 )( 0 ).Pattern = "^([AUT]\d{5}(,|$)){1,5}$" ' evertjan.
aTests( 0 )( 0 ).Pattern = "^[AUT]\d{5}(?:,[AUT]\d{5}){0,4}$"
' Valid numbers are between 1000 and 9999. Each number needs to be
' separated with a comma. The string can only contain a maximum of 10
' numbers.
aTests( 1 )( 0 ).Pattern = "^([1-9]\d{4}(,|$)){1,10}$" ' evertjan. (error)
aTests( 1 )( 0 ).Pattern = "^([1-9]\d{3}(,|$)){1,10}$" ' evertjan.
aTests( 1 )( 0 ).Pattern = "^[1-9]\d{3}(?:,[1-9]\d{3}){0,9}$"
' Valid numbers are between 2000 and 2020. Only 1 number is allowed.
aTests( 2 )( 0 ).Pattern = "^(?:20[01]\d)|(?:2020)$"
Dim aTest, sTest
For Each aTest In aTests
WScript.Echo String( 78, "-" )
WScript.Echo aTest( 0 ).Pattern
For Each sTest In aTest( 1 )
WScript.Echo " " & CStr( aTest( 0 ).Test( sTest ) ) & vbTab & sTest
Next
Next