Wayne Lim
Wayne Lim

Reputation: 13

Validating a string's first 3 letters as uppercase with regex

I have a question on Classic ASP regarding validating a string's first 3 letters to be uppercase while the last 4 characters should be in numerical form using regex.

For e.g.:

dim myString = "abc1234"

How do I validate that it should be "ABC1234" instead of "abc1234"?

Apologies for my broken English and for being a newbie in Classic ASP.

Upvotes: 0

Views: 515

Answers (2)

Bond
Bond

Reputation: 16321

@ndn has a good regex pattern for you. To apply it in Classic ASP, you just need to create a RegExp object that uses the pattern and then call the Test() function to test your string against the pattern.

For example:

Dim re
Set re = New RegExp
re.Pattern = "^[A-Z]{3}.*[0-9]{4}$"  ' @ndn's pattern

If re.Test(myString) Then
    ' Match. First three characters are uppercase letters and last four are digits.
Else
    ' No match.
End If

Upvotes: 2

ndnenkov
ndnenkov

Reputation: 36110

^[A-Z]{3}.*[0-9]{4}$

Explanation:

  1. Surround everything with ^$ (start and end of string) to ensure you are matching everything
  2. [A-Z] - gives you all capital letters in the English alphabet
  3. {3} - three of those
  4. .* - optionally, there can be something in between (if there can't be, you can just remove this)
  5. [0-9] - any digit
  6. {4} - 4 of those

Upvotes: 1

Related Questions