# Developers Planet

Your answer is one click away!

Bry February 2016

### Regular expression match numbers

My regular expression knowledge is poor however I have worked out what the following expression does but not sure how to modify it.

this is the expression `^[0-9]{6,15}\$`

which matches 6 numbers and make sure there is nothing else at the end of the string for example it matches 123456

However I need it to match 123456 and 123456-1

### Answers

Robin Davis February 2016

Let's break this down: `^[0-9]{6,15}\$`

• `^` : beginning of line
• `[0-9]` : a character class representing any character in the range 0-9.
• `{6,15}` : match between 6 and 15 (inclusive I think) occurrences of whatever the previous thing is (in this case it was a character class).
• `\$` : end of line

We want to add optional matching for something like `-[0-9]`. We can add an optional match using `?`.

All together now:

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

The `(-[0-9])?` means "optionally match a dash followed by a single digit".

#### Post Status

Asked in February 2016
Viewed 3,491 times
Voted 7
Answered 1 times