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
Related
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
The following regex working as expected other than the case that it's not allowed that all characters are the same characters.
^(?=[A-Z0-9]+[ -]?[A-Z0-9]+)(?!([A-Z0-9])(?:\1|[ -]){5,10}).{5,10}$
here minimum is 5 characters and the maximum is 10 characters
11114 allowed its minimum length matched as 5 and one charcter is diff so not all same charcters
11111115 allowed as one charcter is different and its more than 5 charcter.
2222222 not allowed as all are same characters
222-22 not allowed as all are same charcters
111-3 allowed as length 5 and one character is different
444-45 allowed as length more than 5
1234565436 allowed as length with in range 5 to 10
There is no need to repeat range quantifier {5,10} multiple times as that makes changing this regex harder for other cases.
You may use this regex for this:
^(?=.{5,10}$)([A-Z0-9])(?!(?:[ -]?\1)+$)[A-Z0-9]*[ -]?[A-Z0-9]+$
RegEx Demo
RegEx Breakup:
^: Start
(?=.{5,10}$): Assert that we have 5 to 10 chars till end
([A-Z0-9]): Match a letter or digit and capture in group #1
(?!(?:[ -]?\1)+$): Negative lookahead to fail the match if same captured value is repeated till end
[A-Z0-9]*: Match 0 or more letter or digit
[ -]?: Match optional space or hyphen
[A-Z0-9]+: Match 1 or more letter or digit
$: End
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.
I'm working on a new regex to prepare for a host-name on a virtual machine, however, I'm running into issue on how to limit character length of 24 while making sure the last character is not a dot or a minus. (the first character must be an alpha character)
I have gotten as far as making sure the first character is an alpha. The second group of characters are 23 in length with [a-zA-z0-9] including the dot and minus. I've tried the negative look-behind .+(?<!-|\.)$ in addition but does not work.
^[a-zA-Z]([a-zA-Z0-9-.]{0,23}
I expect the output of a123456789012345678911234 to be correct already.
I expect this output should be incorrect a12345678901234567891123-
You may use
^[a-zA-Z](?:[a-zA-Z0-9.-]{0,22}[a-zA-Z0-9])?$
See the regex demo and the regex graph:
Details
^ - start of string
[a-zA-Z] - a letter
(?:[a-zA-Z0-9.-]{0,22}[a-zA-Z0-9])? - an optional sequence of:
[a-zA-Z0-9.-]{0,22} - 0 to 22 letters, digits, . or - chars
[a-zA-Z0-9] - a letter or digit
$ - end of string.
In order to limit the number of characters, the expression must be enclosed in ^ and $ denoting the beginning and the end.
^[a-zA-Z][a-zA-Z0-9.-]{0,22}[a-zA-Z0-9]$
[] defines one character from those in parentheses
{a, b} defines the number of occurrences of the preceding character from 0 to 22 in the example
a total limit of 2 to 24 characters
This can be saved shorter but in this way it is the easiest to understand.
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