Link to home
Start Free TrialLog in
Avatar of VBdotnet2005
VBdotnet2005Flag for United States of America

asked on

If alph to numberic in regex

I want to check to see if my first five string is alpha and the next five is numberic using Regex.
ABCDE12345
Avatar of Terry Woods
Terry Woods
Flag of New Zealand image

Use pattern:
^[A-Z]{5}\d{5}$
This should do it:

[A-Z]{5}[0-9]{5}

Open in new window

In code I think it goes something like this...

If Regex.Match(str, "^[A-Z]{5}\\d{5}$").Success Then
     strErr = "Error!"
End If
Oops, that would be:

If Regex.Match(str, "^[A-Z]{5}\\d{5}$").Success Then
     strMsg = "Great!"
End If
My solution was missing the leading ^ to anchor the search at the start of the string.  @TerryAtOpus got it right.

You only need the "$" at the end of the pattern if you do not want any characters to be allowed after the five digits.  (That was not in your problem description.)

^[A-Z]{5}[0-9]{5}

Open in new window

Avatar of VBdotnet2005

ASKER

I can use this, correct? I am not sure what this does  regexoptions.complied?
private reg_str as new regex("^[A-Z]{5}[0-9]{5}$", regexoptions.complied)

if reg_str.ismatch(mystring) then
...
end if
^[A-Z]{5}\\d{5}$")  I don't understand this
^[A-Z]{5}[0-9]{5}  , I understand this

sorry, I am new to regex
\d means a digit. The \ is backslashed because of the way .NET handles strings.
(so \\d and [0-9] are the same thing)
As for the question about regexoptions, you might like to use something like this if you want to ignore case:

Dim options As RegexOptions = RegexOptions.IgnoreCase
If Regex.Match(str, "^[A-Z]{5}\\d{5}$").Success Then
     strMsg = "Great!"
End If
Sorry, should be:

Dim options As RegexOptions = RegexOptions.IgnoreCase
If Regex.Match(str, "^[A-Z]{5}\\d{5}$", RegexOptions).Success Then
     strMsg = "Great!"
End If
ASKER CERTIFIED SOLUTION
Avatar of Terry Woods
Terry Woods
Flag of New Zealand image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial