A question about Regrex

Hi experts
I am using this regrex expression:
   
Regex(@"^[A-F0-9I#*-^\b]*$");

Open in new window


My intention was to restricted inputs to '0'-'9', 'A'-'F', 'I', 'X', '#', '*'.
But it still allows me to enter 'G', 'H', etc.
Could someone tell me where I had done incorrectly.

Thanks.
dominicwongAsked:
Who is Participating?
 
ozoCommented:
The range *-^ includes 'G', 'H', etc.
If your intention was to restricted inputs to'0'-'9', 'A'-'F', 'I', 'X', '#', '*', '-', '^', '\b'
then you could backquote the last -
0
 
käµfm³d 👽Commented:
Just to add to ozo's comment, a hyphen inside of square brackets indicates a range of characters to find. You would need to look at the ASCII table to see which characters fall into your range. To avoid having the regex engine interpret a hyphen as a range, you either need to escape it (or "backquote" it, as ozo refers to it) by using a backslash, or you need to place it as either the very first character or the very last character of your bracket expression. So in other words, all of the following are exactly equivalent:

Regex(@"^[A-F0-9I#*\-^\b]*$");
Regex(@"^[-A-F0-9I#*^\b]*$");
Regex(@"^[A-F0-9I#*^\b-]*$");

Open in new window

0
 
Terry WoodsIT GuruCommented:
No points please because @ozo has solved it.

Just wanted to say that the code change should look like this:
Regex(@"^[A-F0-9I#*\-^\b]*$");

Open in new window

0
 
dominicwongAuthor Commented:
Thanks ozo for your quick response and help.
Thanks kaufmed for going the extra mile to give me a very good explanation.
Thanks also to Terry for your help.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.