PHP Regular Expression - {0,1}, ?, etc

agent provocateur

Regular expression pattern:

[[1-9]-]?[a-zA-Z1-9]

Shouldn't the following strings match?:

2-
5-
2-S
5-t
S
t

What am I not understanding here?


Also Tried:

[[1-9]-]{0,1}[a-zA-Z1-9]
[[1-9]-|][a-zA-Z1-9]
[[[1-9]-]|][a-zA-Z1-9]

Goal

Trying to come up with a pattern for:

1st character = Number 1-9
2nd character = always "-"
3rd character = Alphanumeric (all cases, 1-9)
4rd character = Alphanumeric (all cases, 0-9) Repeat X number of times
OR
1rd character = Alphanumeric (all cases, 1-9)
2rd character = Alphanumeric (all cases, 0-9) Repeat X number of times

(i realize i haven't implemented the repeat portion in the code given here yet)


anubhava

This regex should work for you (assuming X=8):

^([1-9]-)?[a-zA-Z1-9][a-zA-Z0-9]{8}$

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related