Well I'm trying to make a regular expression that only allows me numbers greater than 16 bits (in binary).
That is to say that the regular expression allows numbers greater than 1111 1111 1111 1111
that is>= 1 0000 0000 0000 0000
The expression I have is the following:
(1)* (1|0)* (0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1) (0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)
If anyone can help me validate this I would appreciate it.
The regular expression is fine. I recommend only considering the numbers that start with the most significant digit in 1. In this way, the numbers that start with 0 to the left (which does not contribute to the number) are considered invalid.
I recommend using positive closure (the
+
) to make sure there is at least one one.And be very careful with the spaces , since the regular expression you show has the problem that it forces spaces to exist in the number.
It would look like this:
(1)+(1|0)*(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)
Depending on where this regular expression is executed (if it is in a language like Javascript or in a tool like VisualStudio Code or Notepad++) they will have extensions to the syntax to be able to express those 16 digits in another way.
For example, in Javascript the exact same regular expression can be written like so:
(1)+(1|0)*(0|1){16}
Which is clearly much more concise.
Try the following regular expression:
1[10]{16,}
. In my opinion, simpler.Explained:
show
Important:
Both this solution and the accepted solution would find the number
10000000000000000
inside12310000000000000000321
Which may not be correct for your case since that would be a decimal number.
If you need to restrict this case, you can do it with this regular expression:
A) For regular expressions compatible with PCRE (like php, perl, python,...)
(?<!\d)1[10]{16,}(?!\d)
show
You could also use option B for these languages, it's just that option A is shorter.
B) For regular expressions NOT compatible with PCRE, which do not support lookbehind (like javascript)
(?:^|[^\d\n])(1[10]{16,}(?!\d))
In this case we will look at the first capturing group.
show