Re: Help with RegExp

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



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
.



Relevant Pages

  • str.scan
    ... Regexp or a String). ... added to the result array or passed to the block. ... If the pattern ...
    (comp.lang.ruby)
  • Re: Interaction between two strings
    ... then the string XYZC should give a match. ... > string that is supplied separately from the regex. ... $patterns, you splice them all into one very big $pattern. ... Doing this is immensely faster than iterating through an array or the like: ...
    (comp.lang.perl.misc)
  • Re: regular expression question
    ... how would the pattern need look if the name wasn't "" delimited. ... >> into an array of two values. ... > You can use the following RegEx to parse the string: ... > You will not have a match, so yes you will have an empty string returned. ...
    (microsoft.public.dotnet.languages.vb)
  • Passing functions as arguments
    ... I'm just starting out with ruby. ... I'm trying to make an array of arrays containing [a pattern, a string, ...
    (comp.lang.ruby)
  • Theory behind proof of concept for automatic IVTC decimation
    ... This relates to my video editing project. ... with one character for the pattern.. ... because array indices ... just like video frame counts. ...
    (comp.lang.ruby)