• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 225
  • Last Modified:

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
0
VBdotnet2005
Asked:
VBdotnet2005
  • 8
  • 2
  • 2
1 Solution
 
Terry WoodsIT GuruCommented:
Use pattern:
^[A-Z]{5}\d{5}$
0
 
sjklein42Commented:
This should do it:

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

Open in new window

0
 
Terry WoodsIT GuruCommented:
In code I think it goes something like this...

If Regex.Match(str, "^[A-Z]{5}\\d{5}$").Success Then
     strErr = "Error!"
End If
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
Terry WoodsIT GuruCommented:
Oops, that would be:

If Regex.Match(str, "^[A-Z]{5}\\d{5}$").Success Then
     strMsg = "Great!"
End If
0
 
sjklein42Commented:
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

0
 
VBdotnet2005Author Commented:
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
0
 
VBdotnet2005Author Commented:
^[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
0
 
Terry WoodsIT GuruCommented:
\d means a digit. The \ is backslashed because of the way .NET handles strings.
0
 
Terry WoodsIT GuruCommented:
(so \\d and [0-9] are the same thing)
0
 
Terry WoodsIT GuruCommented:
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
0
 
Terry WoodsIT GuruCommented:
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
0
 
Terry WoodsIT GuruCommented:
Note I'm not a VB.NET programmer - my forte is regular expressions (which are pretty similar across languages). The code I provide is based on other answers so might be a little buggy!
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

  • 8
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now