Regular expression problem when checking the "/" character

I'm having trouble with regular expressions. I want to check user input when entering a bus number.

Examples: 37, 37A, 37S, 37A / L, 16A / 250, 16A / 250K, etc.

The regular expression that I came up with is

(^\d{1,3}[AZ]{0,3})|(^[\d{1,3}[AZ]{0,3}\/\d{0,3}[AZ]{0,3}]) 

He checks 37, 37A, 37S, but when it comes to checking 37A / L, he fails. Can someone tell me where I am going wrong?

Note. I am using a regular expression checker for a text field. I put this in a ValidationExpression expression.

+4
source share
3 answers

He had a problem because the end of the line was not specified.

Replace it

 (^\d{1,3}[AZ]{0,3}$)|(^\d{1,3}[AZ]{0,3}\/\d{0,3}[AZ]{0,3}$) 

Hope this helps.

+2
source

You do not need to hide the / character, try the following:

 (^\d{1,3}[AZ]{0,3}(?:/\d{0,3}[AZ]{0,3})?) 

You can additionally match that the entire string matches the regular expression by specifying the EOS character:

 (^\d{1,3}[AZ]{0,3}(?:/\d{0,3}[AZ]{0,3})?$) 

NB, as @MaxwellTroyMiltonKing points out in the comments, brackets around the entire regex are not needed:

 ^\d{1,3}[AZ]{0,3}(?:/\d{0,3}[AZ]{0,3})?$ 
+2
source

Try the following:

 ^(\d{1,3}[AZ]{0,3})(\/\d{0,3}[AZ]{0,3})?$ 
0
source

All Articles