regex - Regular expression match numbers -


my regular expression knowledge poor have worked out following expression not sure how modify it.

this expression ^[0-9]{6,15}$

which matches 6 numbers , make sure there nothing else @ end of string example matches 123456

however need match 123456 , 123456-1

let's break down: ^[0-9]{6,15}$

  • ^ : beginning of line
  • [0-9] : character class representing character in range 0-9.
  • {6,15} : match between 6 , 15 (inclusive think) occurrences of whatever previous thing (in case character class).
  • $ : end of line

we want add optional matching -[0-9]. can add optional match using ?.

all now:

^[0-9]{6,15}(-[0-9])?$

the (-[0-9])? means "optionally match dash followed single digit".


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -