Regex allow a string to only contain numbers 0 - 9 and limit length to 45

RegexStringString Matching

Regex Problem Overview


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!

Regex Solutions


Solution 1 - Regex

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.

Solution 2 - Regex

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].

Solution 3 - Regex

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}$

Solution 4 - Regex

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...).

Solution 5 - Regex

A combination of both attempts is probably what you need:

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

Solution 6 - Regex

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

Solution 7 - Regex

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

Solution 8 - Regex

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNewToRegExView Question on Stackoverflow
Solution 1 - RegexcodaddictView Answer on Stackoverflow
Solution 2 - RegexeumiroView Answer on Stackoverflow
Solution 3 - RegexChamly IdunilView Answer on Stackoverflow
Solution 4 - RegexBoltClockView Answer on Stackoverflow
Solution 5 - RegexLukeHView Answer on Stackoverflow
Solution 6 - RegexKlaus Byskov PedersenView Answer on Stackoverflow
Solution 7 - RegexNoDisplayNameView Answer on Stackoverflow
Solution 8 - RegexSridharKrithaView Answer on Stackoverflow