64

I am trying to create a regex to have a string only contain 0-9 as the characters and it must be at least 1 char in length and no more than 45. so example would be 00303039 would be a match, and 039330a29 would not.

So far this is what I have but I am not sure that it is correct

[0-9]{1,45} 

I have also tried

^[0-9]{45}*$

but that does not seem to work either. I am not very familiar with regex so any help would be great. Thanks!

codaddict
  • 429,241
  • 80
  • 483
  • 523
NewToRegEx
  • 641
  • 1
  • 5
  • 3

8 Answers8

174

You are almost there, all you need is start anchor (^) and end anchor ($):

^[0-9]{1,45}$

\d is short for the character class [0-9]. You can use that as:

^\d{1,45}$

The anchors force the pattern to match entire input, not just a part of it.


Your regex [0-9]{1,45} looks for 1 to 45 digits, so string like foo1 also get matched as it contains 1.

^[0-9]{1,45} looks for 1 to 45 digits but these digits must be at the beginning of the input. It matches 123 but also 123foo

[0-9]{1,45}$ looks for 1 to 45 digits but these digits must be at the end of the input. It matches 123 but also foo123

^[0-9]{1,45}$ looks for 1 to 45 digits but these digits must be both at the start and at the end of the input, effectively it should be entire input.

codaddict
  • 429,241
  • 80
  • 483
  • 523
  • ahh thank you, can you explain what the carrot ^ and $ do for this expression? Just start and end of a string? – NewToRegEx Oct 19 '10 at 12:02
  • @NewToRegEx - yes. They do not allow any other characters appear before or after the match of 1-45 digits. – eumiro Oct 19 '10 at 12:05
  • 4
    If you live in a unicode world (which most people do), `\d` is **not** the same as `[0-9]`. `\d` allows for `௪` etc (and thousands more) to match; `[0-9]` does not. – Marc Gravell Jul 03 '14 at 07:30
14

Use this regular expression if you don't want to start with zero:

^[1-9]([0-9]{1,45}$)

If you don't mind starting with zero, use:

^[0-9]{1,45}$
Robbie JW
  • 639
  • 1
  • 9
  • 21
Chamly Idunil
  • 1,698
  • 1
  • 15
  • 29
14

The first matches any number of digits within your string (allows other characters too, i.e.: "039330a29"). The second allows only 45 digits (and not less). So just take the better from both:

^\d{1,45}$

where \d is the same like [0-9].

eumiro
  • 194,053
  • 32
  • 286
  • 259
  • 1
    If you live in a unicode world (which most people do), `\d` is **not** the same as `[0-9]`. `\d` allows for `௪` etc (and thousands more) to match; `[0-9]` does not. – Marc Gravell Jul 03 '14 at 07:30
5

codaddict has provided the right answer. As for what you've tried, I'll explain why they don't make the cut:

  • [0-9]{1,45} is almost there, however it matches a 1-to-45-digit string even if it occurs within another longer string containing other characters. Hence you need ^ and $ to restrict it to an exact match.

  • ^[0-9]{45}*$ matches an exactly-45-digit string, repeated 0 or any number of times (*). That means the length of the string can only be 0 or a multiple of 45 (90, 135, 180...).

BoltClock
  • 665,005
  • 155
  • 1,345
  • 1,328
4

A combination of both attempts is probably what you need:

^[0-9]{1,45}$
LukeH
  • 252,910
  • 55
  • 358
  • 405
2

Rails doesnt like the using of ^ and $ for some security reasons , probably its better to use \A and \z to set the beginning and the end of the string

NoDisplayName
  • 14,723
  • 11
  • 59
  • 95
2

^[0-9]{1,45}$ is correct.

Klaus Byskov Pedersen
  • 111,411
  • 28
  • 180
  • 220
-1

For this case word boundary (\b) can also be used instead of start anchor (^) and end anchor ($):

\b\d{1,45}\b

\b is a position between \w and \W (non-word char), or at the beginning or end of a string.

SridharKritha
  • 6,617
  • 2
  • 39
  • 38