none
VBScript: Regular Expression fails on the number "3"? RRS feed

  • Question

  • I'm using a regular expression to evaluate a pattern which consist of a number 1-25, then a [space] and a single alphanumeric character (e.g. 1 A; 10 2; 24 C: ...). Below is my test pattern and everything works except for the number "3" in the range of [1-24]. For the first number 1-24, all numbers work except "3"?

    oRegExp.Pattern = "[1-24]{1,2} \w"
    
    
    Dim str : str = "3 6"
    
    If oRegExp.Test(str) = False then ...
    


    SV

    Friday, August 19, 2016 7:47 PM

Answers

  • Set regEx = New RegExp
    Str = "24 A"
    regEx.Pattern = "[1-2][0-9]? [A-Za-z]"
    regEx.IgnoreCase = True
    regEx.Global = True
    If regEx.Test(Str) = True then Msgbox "Match"

    Patterns are characters not numbers. Above test first char is a 1 or 2. Second character is between 0-9 but the ? changes it to 0 or 1 time only. Then a space. Then A-Za-z to match all alphabetical characters (order is important specify by Unicode sort order - for normal letters that is same as ASCII code - Uppercase first)


    David Candy

    • Marked as answer by svMSDN Tuesday, August 30, 2016 4:43 PM
    Monday, August 22, 2016 1:33 AM

All replies

  • The bit "1-2" means "all characters between 1 and 2 inclusive," which is just 1 and 2. I would guess neither 3 nor 6 works there.

    https://msdn.microsoft.com/en-us/library/20bw873z(v=vs.110).aspx

    What you need is to use Split() to break the space-separated values, then parse the numbers using CInt(), and compare against the range.


    KeepMyIdentities, Your Key to Password Security. Available now on the Windows Store: http://apps.microsoft.com/webpdp/en-US/app/keepmyidentities/61a9f340-97ac-4666-beab-39f9246cb6fa


    Friday, August 19, 2016 8:06 PM
  • Set regEx = New RegExp
    Str = "24 A"
    regEx.Pattern = "[1-2][0-9]? [A-Za-z]"
    regEx.IgnoreCase = True
    regEx.Global = True
    If regEx.Test(Str) = True then Msgbox "Match"

    Patterns are characters not numbers. Above test first char is a 1 or 2. Second character is between 0-9 but the ? changes it to 0 or 1 time only. Then a space. Then A-Za-z to match all alphabetical characters (order is important specify by Unicode sort order - for normal letters that is same as ASCII code - Uppercase first)


    David Candy

    • Marked as answer by svMSDN Tuesday, August 30, 2016 4:43 PM
    Monday, August 22, 2016 1:33 AM