VBdotnet2005
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
ABCDE12345
This should do it:
[A-Z]{5}[0-9]{5}
In code I think it goes something like this...
If Regex.Match(str, "^[A-Z]{5}\\d{5}$").Succes s Then
strErr = "Error!"
End If
If Regex.Match(str, "^[A-Z]{5}\\d{5}$").Succes
strErr = "Error!"
End If
Oops, that would be:
If Regex.Match(str, "^[A-Z]{5}\\d{5}$").Succes s Then
strMsg = "Great!"
End If
If Regex.Match(str, "^[A-Z]{5}\\d{5}$").Succes
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.)
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}
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
private reg_str as new regex("^[A-Z]{5}[0-9]{5}$"
if reg_str.ismatch(mystring) then
...
end if
ASKER
^[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
^[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}$").Succes s Then
strMsg = "Great!"
End If
Dim options As RegexOptions = RegexOptions.IgnoreCase
If Regex.Match(str, "^[A-Z]{5}\\d{5}$").Succes
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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
^[A-Z]{5}\d{5}$