conditional with no leading zeros in decimal number using regular expression - regex

Below is my regular expression I am working with
^\d+\.?((25)|(5)|(75)|0+)?$ which should be allowing numbers like
0010,10.0,0.0,0,0.25,1.0,22,1.5,11.25,23.75,22.000 etc.
I don't want to allow like
0017.5,0010.0 etc with leading 0s in decimal numbers.
How can I make that?

Provided you may use lookaheads in the regex pattern, you may use
^(?!0+[1-9]\d*\.\d)\d+(?:\.(?:[27]?5|0+))?$
See the regex demo. The (?!0+[1-9]\d*\.\d) negative lookahead will fail the match if the strings starts with one or more zeros and the number is itself a number with a fractional part.
Regex details
^ - start of string
(?!0+[1-9]\d*\.\d) - a negative lookahead that fails the match if, immediately at the start of the string, there is
0+ - one or more 0 chars
[1-9] - a digit from 1 to 9
\d* - 0+ digits
\. - a dot
\d - a digit
\d+ - 1+ digits
(?:\.(?:[27]?5|0+))? - an optional string that matches 1 or 0 occurrences of:
\. - a dot
(?:[27]?5|0+) - 2 or 7 (optionally) and then 5, or one or more 0 chars
$ - end of string.

Give a range to the starting number instead of allowing any number and allow 0 or more numbers after that number but before the comma. Remove the ?..? quantifier because you want to make sure it is a decimal number. To account for whole numbers add |(\d+). And to account for numbers between 0 and 1, add ([0]\.((25)|(5)|(75)|0+)) This becomes:
^(([0]\.((25)|(5)|(75)|0+))|([1-9]\d*\.((25)|(5)|(75)|0+))|(\d+))$

Related

validate string data using regex which allowed only numbers and - (hyphen) of length 6 but hypen should not be the end and the digits not same

Validate the string using the regex which has the - (hypen)
Requirement is : string contains only digits and - (hyphens) and not end with - (hyphen) and all other digits not be the same.
^([0-9-])(?!\1+$)[0-9-]{5}$
The above one allow only digits and hyphen but its not restricted end with hyphen and check all other digits are same.
ex:
1111-1 Not allowed because all are same digits
1111-2 Allowed
11112- Not allowed as its end with - Hypen
-12345 Not allowed as its start with - hypen
You might write the pattern as
^(\d)(?!(?:\1|-)+$)(?!\d*-\d*-)[\d-]{4}\d$
Explanation
^ Start of string
(\d) Capture a single digit in group 1
(?! Negative lookahead
(?:\1|-)+$ Check that to the right there is not only the group 1 value or hyphens
(?!\d*-\d*-) Assert not 2 hyphens
) Close lookahead
[\d-]{4} Match 4 digits or hyphens
\d Match a digit
$ End of string
Regex demo
If there should be at least 1 hyphen:
^(\d)(?!(?:\1|-)+$)(?=\d*-)[\d-]{4}\d$
Regex demo
My 2 cents to allow [01] hyphens:
^(?=.{6}$)(\d)(?=.*(?!\1)\d)\d+(?:-\d+)?$
See an online demo

Regex for numeric value that could contain comma & dots

I have been trying but without success
I need a regular expression for validating numbers that could contain dots and commas,
the number should be positive and there should be max of two numbers after the comma
Valid cases would be:
1000 - valid
1,000 - valid
1,000.22 - valid
-2 not valid
1,000.233 not valid
0 not valid
1.00,22 - not valid
Language is javascript
let valid =["1000","1,000","1,000.22"];
let notValid = ["-2","1,000.233 ","0","1.00,22"];
let rge = /^[1-9]+\d*(,\d{3})*(\.\d{1,2})?$/;
for(let x of valid)
console.log(x," è valida? ",rge.test(x));
for(let x of notValid)
console.log(x," è valida? ",rge.test(x));
Above there is a possible solution in Javascript, you haven't specified the language.
\d are numbers in the range [0-9]
The full stop . is a metacharacter (it means any character), to refer to the character . you have to escape it thereby \.
+ means at least 1 or more times
* means 0 or more times
? means 0 or 1 time
{1,2} means match minimum 1 time, maximum 2 times
The starting ^ and final $ refer to a exact matching otherwise you could have a partial matching of the string
A few assumptions:
Invalid: '123456789.12' and '12345,123.12'
I think the following does what you are after:
^[1-9](?:\d*|\d{0,2}(?:,\d{3})*(?:\.\d\d?)?)$
See the online demo
^ - Start-line anchor.
[1-9] - A single digit in the range 1-9.
(?: - Open a non-capture group:
\d* - 0+ Digits to allow any integer.
| - Or:
\d{0,2} - Between 0 to 2 digits;
(?:,\d{3})* - Followed by a non-capture group to allow any 0+ times a comma followed by 3 digits.
(?:\.\d\d?)? - Followed by an optional non-capture group to allow up to two decimals.
)$ - Close non-capture group and match the end-line anchor.
Or, if you also want to allow any integer followed by decimals (e.g: '123456789.01') you may change this to:
^[1-9](?:\d*|\d{0,2}(?:,\d{3})*)(?:\.\d\d?)?$
I think this regex should do the trick:
[1-9][\d,]*(\.\d{1,2})?
[1-9] - matches one character between 1 and 9 at the beginning (required to not match 0)
[\d,]* - matches zero or more digits or commas
(\.\d{1,2})? - zero or one group of a dot and one or two digits
For testing regexes I do recommend https://regex101.com/

Regular Expression Scope Too Large

I have created a (mostly) working regular expression that accepts any number > 0 or <= 12, allowing up to two decimal places. The problem is that it also accepts numbers between 12-13 such as 12.25, and also 0.
My regular expression pattern is /^\b(0*([0-9]|1[0-2]))\b\.?[0-9]{0,2}$/
How can I change this to prevent 0 or numbers greater than 12 from being accepted?
You may use
^(?=[^1-9]*[1-9])0*(?:(?:\d|1[01])?(?:\.[0-9]{0,2})?|12(?:\.0{0,2})?)$
See the regex demo and the regex graph:
If a digit after a decimal separator symbol is required, replace {0,2} with {1,2}:
^(?=[^1-9]*[1-9])0*(?:(?:\d|1[01])?(?:\.[0-9]{1,2})?|12(?:\.0{1,2})?)$
Details
^ - start of string
(?=[^1-9]*[1-9]) - a positive lookahead that requires one non-zero digit (a digit from 1 to 9) after any chars other than digits from 1 to 9
0* - any 0+ leading zeros
(?: - start of a non-capturing group:
(?:\d|1[01])?(?:\.[0-9]{1,2})? - 0 to 11 numbers (matched optionally, see the ? after the first closing parenthesis) followed with an optional sequence of . and 1 to 2 digits
| - or
12(?:\.0{1,2})? - 12 optionally followed with . and 1 to 2 digits
) - end of a non-capturing group
$ - end of string

What is the Regular Expression to allow only one hyphen(-) only between 2 letters or a letter and a number but not 2 numbers

I was using the below pattern.
/^[A-Za-z0-9]+(-[A-Za-z0-9]+)*$/.
What i need is that it should not be allowing the hyphen between 2 numbers.
I know that we have to make modification with 0-9, where we can restrict user from entering them twice.
The (?!.*[0-9]-[0-9]) lookahead after ^ will make sure there is no digit-digit pattern in the string. Also, if there must be 1 or 0 hyphens, replace * at the end with ? (0 or more occurrences).
Use
^(?!.*[0-9]-[0-9])[A-Za-z0-9]+(-[A-Za-z0-9]+)?$
See the regex demo.
Details
^ - start of string
(?!.*[0-9]-[0-9]) - a negative lookahead that fails the match if, after any 0+ chars other than line break chars, there is a digit, hyphen, digit pattern
[A-Za-z0-9]+ - 1 or more ASCII alphanumeric chars
(-[A-Za-z0-9]+)? - 1 or 0 sequences of:
- - a hyphen
[A-Za-z0-9]+ - 1 or more ASCII alphanumeric chars
$ - end of string.

Regex, must contain a number, 1-5 chars and letters and digits

Specs:
1 - 5 chars length
Contains at least 1 number char
Only A-Z, a-z and 0-9 allowed
This is what I have sofar: ^[a-zA-Z0-9]{0,5}$
Problem I have this accepts 'AV' for example. It must contain a number to be valid.
https://regex101.com/r/i5VnXt/1
You need to add a positive lookahead (?=\D*\d) and use {1,5} limiting quantifier at the end to match 1 to 5 chars:
^(?=\D*\d)[a-zA-Z0-9]{1,5}$
See the regex demo
Details
^ - start of a string
(?=\D*\d) - a positive lookahead requiring a digit after 0 or more non-digit symbols
[a-zA-Z0-9]{1,5} - 1 to 5 (due to {1,5} limiting quantifier) consecutive alphanumeric chars (ASCII letters or digits)
$ - end of string.