How do I limit a character appearance in a regex? - regex

I am stuck with a problem that challenges me to create the regular expression for binary numbers (containing 0s and 1s). But the string should only match if the binary number only contains a maximum of five 1s. How do I limit a character appearance in a regex?
Examples:
01101101 is correct
01111100 is correct
10110011 is correct
01111110 is wrong
11111110 is wrong

^0*(?:10*){,5}$
Essentially this matches any combination of '1's and '0's but only allows a substring containing a single '1' character to occur five times at most.
Try it out here:
https://regex101.com/r/JKV1Uk/2
Explanation:
^ matches the beginning of the string
0* matches zero or more '0's
(?:10*){,5} matches up to 5 '1's followed by any number of zeros
$ matches the end of the string

If you engine supports lookaheads, and there can not be more than 8 times a 1 or 0 and the 1 should not occur more than 5 times, you might use:
^(?=[01]{8}$)(?!(?:0*1){6,})[01]+$
Explanation
^ Begin of the string
(?= Positive lookahead that asserts that what is on the right side
[01]{8}$ Match 8 times a 0 or 1 until the end of the string
) Close lookahead
(?! Negative lookahead that asserts that what is on the right side
(?:0*1){6,} The pattern zero or more times a 0 followed by a 1 0*1 is not repeated 6 or more times (so 0 till 5 times is valid)
) Close negative lookahead
[01]+$ Match 0 or 1 one or more times
$ The end of the string

Related

Regex to match a string containing 14 digits and 1 character at any position

I need a regular expression that matches a string of 15 characters where 14 of them are digits and 1 is a character. The character can be in any position of the string.
I have the following long regex:
^.\d{14}|\d{1}.\d{13}|\d{2}.\d{12}|\d{3}.\d{11}|\d{4}.\d{10}|\d{5}.\d{9}|\d{6}.\d{8}|\d{7}.\d{7}|\d{8}.\d{6}|\d{9}.\d{5}|\d{10}.\d{4}|\d{11}.\d{3}|\d{12}.\d{2}|\d{13}.\d{1}|\d{14}.$
Can it be simplified?
Here is a sample match: 1000-1234567890
(?=^.{15}$)\d{0,14}\D\d{0,14}$
First check the string is 15 characters long, then has 0-14 digits, one non-digit, then 0-14 digits.
This isn't exactly the same as the original regex, which allows 15 digits in a row. To get that, simply change \D to .
We can use a lookaround trick here:
^(?!(?:.*\D){2})(?=.{15}$)\d*\D\d*$
This regex pattern says to match:
^ from the start of the string
(?!(?:.*\D){2}) assert that 2 or more non digits do NOT occur (implying at most 1 occurs)
(?=.{15}$) assert length is 15 characaters
\d*\D\d* then a non digit surrounded, possibly, on either side by numbers
$ end of the string

Regex count number of digits excluding certain characters

I have a regex with certain conditions
Only allow digits and some characters ie space . + ( - ) x X
Total digits excluding the characters must be between 8 to 20
I have the first condition, 2nd seems bit tricky for me
^[+\d\s\-\.\(\)xX]{8,20}$
Thank you for your time.
^[ \.\+\(\-\)xX]*(?:\d[ \.\+\(\-\)xX]*){8,20}$
[ \.\+\(\-\)xX]* matches any number of the nonnumeric characters you've listed. We start by doing this at the beginning of the string. (This part can be removed if the string is not allowed to start with one of those characters.)
We then match 8-20 digits, each of which can be followed by any number of said nonnumeric characters.
The 8 to 20 digits requirement is a bit ugly, but we can handle that using positive and negative lookaheads. Consider this version:
^(?=(?:\D*\d){8})(?!(?:\D*\d){21})[\d\s.()xX+-]+$
Explanation:
^ from the start of the input
(?=(?:\D*\d){8}) assert that 8 or digits appear
(?!(?:\D*\d){21}) but assert that more than 20 digits do NOT appears
[\d\s.()xX+-]+ match numbers, whitespace, (), +, x, X or - 1 or more times
$ end of the input

Regex expression for numbers and leading zeros just with a dot and decimal

I'm trying to find a regex for numeric inputs. We can receive a leading 0 just if we add a dot for adding 1 or 2 decimal numbers. And of course just accept numbers.
These are the scenarios that we can accept:
0.01
1.1
1.02
120.01
We can't accept these values
0023
0100
.01
.12
Which regex is the best option for these cases?
Until now we try we the following regex for accepting just number and dots
[A-Za-z,]
And also we try with the following ones:
^[+-]?[0-9]{1,3}(?:[0-9]*(?:[.,][0-9]{1})?|(?:,[0-9]{3})*(?:\.[0-9]{1,2})?|(?:\.[0-9]{3})*(?:,[0-9]{1,2})?)$
"/^[-]?[$]\d{1,3}(?:,?\d{3})*\.\d{2}$/"
"/(^(\d{1})\.{0,1}([0-9]){0,2}$)|(^([1-9])\d{0,2}(\,\d{0,3})$)/g"
(?:0|[1-9][0-9]*)(?:\.[0-9]{1,2})?
And the next one for deleting the leading zeros but it didn't work for 0.10 cases
^0+
If a negative lookahead is supported, you can exclude matches that start with a zero and have no decimal part.
^(?!0\d*$)\d+(?:\.\d{1,2})?$
^ Start of string
(?!0+\d*$) Negative lookahead, assert not a zero followed by optional digits at the right
\d+ Match 1+ digits
(?:\.\d{1,2})? Match an optional decimal part with 1 or 2 digits
$ End of string
Regex demo
I would go with ^(0|[1-9]\d*|(0|[1-9]\d*)\.\d+)$
You can test here: https://regex101.com/r/oNMgR9/1
Explanation
^ means : match the beginning of the string (or line if the m flag is enabled).
$ means : match the end of the string (or line if the m flag is enabled).
(a|b) means match "a" or match "b" so I'll use this to match either "0" alone or any number not starting with a "0". It's the syntax for a logical or.
. alone is used to match any char. So you have to escape it if you want to match the dot character. This is why I wrote 0\. instead of 0..
[ ] is used to list some characters you want to match. It can be a range if you use the - char, so [1-9] means any digit char from "1" to "9".
\d is to match a digit. It's totally equivalent to [0-9].
* means : match the preceding pattern 0 or many times, so \d* means that it will match 0 or many times a digit, so it will match "8" or "465" or "09" but also an empty string "". If you want to match the preceding pattern at least once or many times then you use + instead of *. So \d+ won't match an empty string "" but \d* would match it.
A) Just a number not starting with 0
[1-9]\d* will match any digit from 1 to 9 and then optionnaly followed by other digits. This will match numbers without a decimal point.
B) Just 0
0 alone is a possibility. This is because the case above isn't covering it.
B) A number with decimals
(0|[1-9]\d*)\.\d+ will match either a "0" alone or a number not starting by "0" and then followed by a point and some other digits (which have to be present because we don't want to match "45." without the numbers behind the dot).
Better alternative
The solution from #TheFourthBird is a bit cleaner with the use of a negative lookahead. It's just a bit different to understand. And he read the question completely: You wanted 1 or 2 digits after the decimal. I forgot about that, so, effectively, \d+ should be replaced by \d{1,2} as you don't want more than 2 digits.
You can use
^(?![0.]+$)(?:[1-9]\d*|0)(?:\.\d{1,2})?$
See the regex demo.
Details:
^ - start of string
(?![0.]+$) - fail the match if there are just zeros or dots till end of string
(?:[1-9]\d*|0) - either a non-zero digit followed with any zero or more digits or a zero
(?:\.\d{1,2})? - optionally followed with a sequence of a . and one or two digits
$ - end of string.

Regular expression 2 digits 10 letters/digits

I'm trying to make a regular expression, but something isn't working for me, the requirements are the following:
Min length is 1
Max length is 12
The first 2 symbols must be numbers
Next 10 must be either letters or numbers
This is what I have so far
/^[0-9]{0,2}[a-z][A-Z][0-9]{0,10}$/
Can you guys tell me what I'm doing wrong?
Your pattern ^[0-9]{0,2}[a-z][A-Z][0-9]{0,10}$ matches 0, 1 or 2 digits at the start.
Then it matches 2 chars [a-z][A-Z] being a lowercase and an uppercase char A-Z which should be present in the string, and also makes the string length at least 2 chars.
You can make the second digit optional, and use 1 character class for the letters or numbers.
The length then has a minumum of 1, and a maximum of 12.
^(?!\d[a-zA-Z])\d\d?[a-zA-Z0-9]{0,10}$
^ Start of string
(?!\d[a-zA-Z]) negative lookahead, assert not a digit followed by a-zA-Z
\d\d? Match 1 or 2 digits
[a-zA-Z0-9]{0,10} Match 0-10 repetitions of any of the listed ranges
$ End of string
Regex demo
Or a version withtout a lookahead as suggested by #Scratte in the comments, matching a single digit and optionally a second digit followed by 0-10 repetitions of the listed ranges:
^\d(?:\d[A-Za-z\d]{0,10})?$
Regex demo

How to match a whole string that contains just two and no more than two digits between 0 and 10 in regex?

This regex does not work for me as selects all groups of two and multiple digits and not the string.
abcde9 = match
abcde12 = not matched
abcde12345678 = not matched
What I have at the moment is this, it I just can't include the 0 and the 10 as two digits numbers in the regex, can anyone help me?
\d{0,10}[1-9]
If you want to match any string containing exactly one integer from 0 to 10 then use
^\D*(\d|10)\D*$
which means "any non-digit content followed by either a single digit or the number 10 and then followed by any non-digit content"
try it at regex101
I think you are looking for
^\D*(?:[0-9]|10)(?:\D+(?:[0-9]|10))?\D*$
See demo
This will match a whole string that contains 1 or 2 whole integer numbers from 0 to 10, and no other digits.
The regex breakdown:
^ - start of string
\D* - 0 or more characters other than digit
(?:[0-9]|10) - numbers from 0 to 10
(?:\D+(?:[0-9]|10))? - 1 or 0 occurrence of
\D+ - 1 or more characters other than digit
(?:[0-9]|10) - numbers from 0 to 10
\D* - 0 or more characters other than digit
$ - end of string
Is that what you looking for:
/(0[1-9])$/
You can test that regex to make sure it fits your needs:
https://regex101.com/r/hX6lB7/3