regexp to validate a non-zero number - javascript

My requirement is to validate a NON-ZERO number. The regular expression that I used is the following.
^[1-9]?\d+(\.\d)?\d*$
VALID VALUES should be
2
22
2222 (up to any number of digits)
2.2222 (up to any number of decimal points)
INVALID VALUES
0
(. without decimal values)
0.1 (any number of decimals, but start digit is 0)
2.4.5 (more than one .)
basically any values starting with 0 or has more than one . or no decimal points provided when . is added, are INCORRECT.

^[1-9]\d*(?:\.\d+)?$
https://regex101.com/r/wHZUoW/1
-Since you don't want the number to start with 0, you shouldn't make the [1-9] at the beginning optional with ?.
-As general good practice, a non-capturing group (?: ... ) is used instead of a capturing group, because the contents do not need to be referenced later.

You can use this regex to validate your numbers:
^(?=[1-9])\d*\.?\d+$
It uses a regex proposed by #WiktorStribiżew as a comment to this question to match decimal numbers (^\d*\.?\d+$), and adds a positive lookahead to ensure that the first character of the number is not 0. Note that if you want to allow numbers such as .3, you should add . to the lookahead character class i.e. ^(?=[1-9.])\d*\.?\d+$
Demo on regex101

[1-9]+[0-9]*(\.[0-9]+)? should work for your case.
[1-9]+ will make sure that the expression starts with a number different than 0.
[0-9]* will make sure to allow the expression to have zeros after the first digit.
(\.[0-9]+)? will allow an extension to the expression, which has to have a . and at least 1 number after it. The ? in the end makes it optional.
By the way, I really like this website to test my regular expressions: https://regexr.com/, you should try it.

Related

Regex-How can I filter out any grouping of 3 numbers? [duplicate]

I'm attempting to string match 5-digit coupon codes spread throughout a HTML web page. For example, 53232, 21032, 40021 etc... I can handle the simpler case of any string of 5 digits with [0-9]{5}, though this also matches 6, 7, 8... n digit numbers. Can someone please suggest how I would modify this regular expression to match only 5 digit numbers?
>>> import re
>>> s="four digits 1234 five digits 56789 six digits 012345"
>>> re.findall(r"\D(\d{5})\D", s)
['56789']
if they can occur at the very beginning or the very end, it's easier to pad the string than mess with special cases
>>> re.findall(r"\D(\d{5})\D", " "+s+" ")
Without padding the string for special case start and end of string, as in John La Rooy answer one can use the negatives lookahead and lookbehind to handle both cases with a single regular expression
>>> import re
>>> s = "88888 999999 3333 aaa 12345 hfsjkq 98765"
>>> re.findall(r"(?<!\d)\d{5}(?!\d)", s)
['88888', '12345', '98765']
full string: ^[0-9]{5}$
within a string: [^0-9][0-9]{5}[^0-9]
Note: There is problem in using \D since \D matches any character that is not a digit , instead use \b.
\b is important here because it matches the word boundary but only at end or beginning of a word .
import re
input = "four digits 1234 five digits 56789 six digits 01234,56789,01234"
re.findall(r"\b\d{5}\b", input)
result : ['56789', '01234', '56789', '01234']
but if one uses
re.findall(r"\D(\d{5})\D", s)
output : ['56789', '01234']
\D is unable to handle comma or any continuously entered numerals.
\b is important part here it matches the empty string but only at end or beginning of a word .
More documentation: https://docs.python.org/2/library/re.html
More Clarification on usage of \D vs \b:
This example uses \D but it doesn't capture all the five digits number.
This example uses \b while capturing all five digits number.
Cheers
A very simple way would be to match all groups of digits, like with r'\d+', and then skip every match that isn't five characters long when you process the results.
You probably want to match a non-digit before and after your string of 5 digits, like [^0-9]([0-9]{5})[^0-9]. Then you can capture the inner group (the actual string you want).
You could try
\D\d{5}\D
or maybe
\b\d{5}\b
I'm not sure how python treats line-endings and whitespace there though.
I believe ^\d{5}$ would not work for you, as you likely want to get numbers that are somewhere within other text.
I use Regex with easier expression :
re.findall(r"\d{5}", mystring)
It will research 5 numerical digits. But you have to be sure not to have another 5 numerical digits in the string

Regex to check whether string has a decimal and two integers at the end

Can someone help me build a Regex that will check whether a string with only numbers and a decimal contains a decimal and then two integers 0-9 at the end?
So that something like the following would pass
3423.32
35232.53
but not
3223
232
232
asfas
232.3
232.3432
232.222
/^\d+\.\d\d$/
should do.
^ start of the string
\d+ selects any amount (actually 1 or more) of number characters
\. the actual '.' character, but because . normally is special in regular expression, it needs the \
\d\d two numbers
$ end of the string
You can always put a regular expression into https://regexr.com/ to see a similar explanation :)

Regex for significant figures of a defined size (any language)

I would have thought this would be fairly common, but haven't found a solution
What I would like is a regular expression that fails on a set number of significant figures (a Max), but passes for less than the Max. I would like it to work with both dot or comma (french) decimal separators.
So for 15 significant figures, these should pass:
0
0.00
1
-1
1.23456789012345
10.2345678901234
12.3456789012345
-123.4
-12.34
-1,33
-1.33
-123456789012345
-1234567890123450
-12345678901234.50
12345678901234.50
123456789012345.00
// should fail:
-1234567890123456
-12345678901234.56
12345678901234.56
123456789012345.60
1.234567890123456
12.34567890123456
123456789012340.6
123456789012300.67
123456789012300000000000.67
10000000000010000000001000010000000001.22
I know I need to use negative look a heads, and I have got close with this so far:
^(?!(?:.*?[1-9]){15,})([-+]?\s*\d+[\.\,]?\d*?)$
https://regex101.com/r/hQ1rP0/218
but you can see the last few still pass, any pointers?
Code
For actual scientific notation (where leading zeros matter before the decimal symbol), you can use the following
^-?(?=\d{1,15}(?:[.,]0+)?$|(?:(?=.{1,16}0*$)(?:\d+[.,]\d+)))‌.+$
This next regex, however, works for your case of leading zeros regardless of decimal position.
See this regex in use here
^-?(?=\d{1,15}(?:[.,]0+)?0*$|(?:(?=.{1,16}0*$)(?:\d+[.,]\d+)‌​)).+$
Explanation
^ Assert position at the start of the line
-? Match zero or one of the - character literally
(?=\d{1,15}(?:[.,]0+)?0*$|(?:(?=.{1,16}0*$)(?:\d+[.,]\d+))) Positive lookahead ensuring what follows matches the following
\d{1,15}(?:[.,]0+)?0*$ Option 1
\d{1,15} Match between 1 and 15 of any digit character
(?:[.,]0+)? Match a decimal symbol ,., followed by one or more 0s literally, but either zero or one time
0* Match any number of 0s literally
$ Assert position at the end of the line
(?:(?=.{1,16}0*$)(?:\d+[.,]\d+)) Option 2
(?=.{1,16}0*$) Ensure what follows matches the following
.{1,16} Match any character between 1 and 16 times
0* Match any number of 0s literally
$ Assert position at the end of the line
(?:\d+[.,]\d+) Match the following
\d+ Match any digit between 1 and unlimited times
[,.] Match a decimal character
\d+ Match a digit between 1 and unlimited times
.+ Match any character one or more times
$ Assert position at the end of the line (not really needed but I think for readability it helps)

Decimal regex & Empty String

I have this javascript Regex (3 decimal places wiht a single dot)
^\d+(\.\d{1,3})?$
I want to also match on an empty string "" which i believe is
^$
How can I combine these into 1 regex
These should be the passing tests
"" //empty string
1
1.
1.0
1.00
1.000
123456789
0
.0
.00
.000
I hope I have covered all of them.
Not including the empty space, your current expression doesn't seem to pass your requirements.
^\d*\.?\d{0,3}$
Optional leading digits, optional point, up to three more digits before the end.
EDIT:
#Guffa noticed that my original solution would also match simply a dot, "."
^\d*((\d\.)|(\.\d))?\d{0,3}$
This version replaces the \.? check with a check for a digit followed by a dot, or a dot followed by a digit, or neither.
Make an expression with three different cases:
zero or more digits
one or more digits, period, zero to three digits
zero or more digits, period, one to three digits
This will pass all your tests, and also the string "." will not pass:
^(\d*|\d+\.\d{0,3}|\d*\.\d{1,3})$
Demo: http://jsfiddle.net/Guffa/9pnwk/
The digit period case was a difficult one, that my original answer missed. This answer is simpler than the others, covers all cases, and doesn't have any false matches.
Start of the string
Match Either
digits 1+ times then optional "."
"." as long as there is a digit ahead
digits 0-3 times
end of string
Expression
^((\d+\.?|\.(?=\d))?\d{0,3})$
REY
I would rather go for a ==="" or your regex comparison, just for performance's sake

UK Currency Regular Expression for javascript

I'm after a regular expression that matches a UK Currency (ie. £13.00, £9,999.99 and £12,333,333.02), but does not allow negative (-£2.17) or zero values (£0.00 or 0).
I've tried to create one myself, but I've got in a right muddle!
Any help greatfully received.
Thanks!
This'll do it (well mostly...)
/^£?[1-9]{1,3}(,\d{3})*(\.\d{2})?$/
Leverages the ^ and $ to make sure no negative or other character is in the string, and assumes that commas will be used. The pound symbol, and pence are optional.
edit: realised you said non-zero so replaced the first \d with [1-9]
Update: it's been pointed out the above won't match £0.01. The below improvement will but now there's a level of complexity where it may quite possibly be better to test /[1-9]/ first and then the above - haven't benchmarked it.
/^£?(([1-9]{1,3}(,\d{3})*(\.\d{2})?)|(0\.[1-9]\d)|(0\.0[1-9]))$/
Brief explanation:
Match beginning of string followed by optional "£"
Then match either:
a >£1 amount with potential for comma separated groupings and optional pence
OR a <£1 >=£0.10 amount
OR a <=£0.09 amount
Then match end of line
The more fractions of pence (zero in the above) you require adding to the regex the less efficient it becomes.
Under Unix/Linux, it's not always possible to type in the '£' sign in a JavaScript file, so I tend to use its hexadecimal representation, thus:
/^\xA3?\d{1,3}?([,]\d{3}|\d)*?([.]\d{1,2})?$/
This seems to take care of all combinations of UK currency amounts representation that I have come across.
/^\xA3?\d{1,}(?:\,?\d+)*(?:.\d{1,2})?$/;
Explanation:
^ Matches the beginning of the string, or the beginning of a line.
xA3 Matches a "£" character (char code 163)
? Quantifier for match between 0 and 1 of the preceding token.
\d Matches any digit character (0-9).
{1,} Match 1 or more of the preceding token.
(?: Groups multiple tokens together without creating a capture group.
\, Matches a "," character (char code 44).
{1,2} Match between 1 and 2 of the preceding token.
$ Matches the end of the string, or the end of a line if the multiline flag (
You could just make two passes:
/^£\d{1,3}(,\d{3})*(\.\d{2})?$/
to validate the format, and
/[1-9]/
to ensure that at least one digit is non-zero.
This is less efficient than doing it in one pass, of course (thanks, annakata, for the benchmark information), but for a first implementation, just "saying what you want" can significantly reduce developing time.

Categories