check validation of an expression with Regex

168 views Asked by At

I need in my code to verify the validity of an expression entered in a textbox, so I thought to Regex but my problem is that I do not get it.

So here is my expression: [3 Numbers]-[1 character Shift].[1 Number].

for example: 007-L.4

I try with this:

Dim MyRegex As Regex = New Regex("^[0-9]{3}-[a-zA-Z].[O-9]$") 

but it does not work

thank you in advance

1

There are 1 answers

4
Lucas Trzesniewski On BEST ANSWER

You have two errors in your pattern:

^[0-9]{3}-[a-zA-Z].[O-9]$
                  ^ ^
                  1 2
  1. The . is a metacharacter which matches any character. You need to escape it to \. to match periods only,
  2. Your range is not valid, since you wrote O (the letter) instead of 0 (the digit). :-)

Here's a the corrected pattern:

Dim MyRegex As Regex = New Regex("^[0-9]{3}-[a-zA-Z]\.[0-9]$")

(demo)