Salary Field to accept amount from 1-infinite with decimal value. only 0 is considered as invalid
[0-9][1-9.]*[0-9]+[1-9]*
'Salary':['',[Validators.pattern('[0-9][1-9.]*[0-9]+[1-9]*')]]
0 as invaid
1 as valid
100 as valid
99909 as valid
1.2 as valid
111.77700 as valid
Your Validators.pattern is rather corrupt as it can match any amount of dots in between two digits and the input cannot contain a single digits.
If you allow 0.000 but not just 0, you may use
Validators.pattern('(?!0$)[0-9]+(?:\\.[0-9]+)?')
If you disallow any zero value, including 0.0000, you need
Validators.pattern('(?!0+(?:\\.0+)?$)[0-9]+(?:\\.[0-9]+)?')
or plain
Validators.pattern('(?![0.]+$)[0-9]+(?:\\.[0-9]+)?')
Note that Angular will enclose the string pattern with anchors automatically: '(?!0+(?:\\.0+)?$)[0-9]+(?:\\.[0-9]+)?' => /^(?!0+(?:\.0+)?$)[0-9]+(?:\.[0-9]+)?$/.
Note you may use a regex literal, too, but you will have to specify the anchors and use single backslashes to define regex escapes:
Validators.pattern(/^(?!0+(?:\.0+)?$)[0-9]+(?:\.[0-9]+)?$/)
See the regex demo.
^ - start of string
(?!0+(?:\.0+)?$) - a negative lookahead that fails the match if there are 1+ zeros followed with an optional sequence of a . and 1+ digits
[0-9]+ - 1+ digits
(?:\.[0-9]+)? - an optional sequence of a . and 1+ digits
$ - end of string.
I have tested the below regex for all the cases you mentioned and more. I hope it helps.
Validators.pattern('^[1-9][0-9]*(\.[0-9])?')]
Check out the image.
Salary regex javascript - image
Assuming the language is JS then this would work:
^((\d*[1-9]+\d*(\.\d+)?)|(\d*\.\d*[1-9]+\d*))$
You may need to escape the backslashes since it appears you are placing the regex inside a JS string:
'Salary':['',[Validators.pattern('^((\\d*[1-9]+\\d*(\\.\\d+)?)|(\\d*\\.\\d*[1-9]+\\d*))$')]]
https://regex101.com/r/4uBK9X/3
Having a very large data set of float values where the precision is not longer needed, what is a regular expression that I can use with BBEdit to allow me to keep a maximum of 5 digits after a period?
Physically, the decimal value always has a character preceeding the period, is always preceeded by a space, but can have a comma or a space after the string.
sample:
-162.40904700399989, -82.896416924999954
You may use
Find: (\d\.\d{5})\d+
Replace: \1
Details
(\d\.\d{5}) - Group 1 (referred to via \1 from the replacement pattern): a digit, . and then 5 digits (note the first \d has no quantifier, we are not interested if there are more than one, one is enough, before the decimal separator)
\d+ - one or more digits. Note the + quantifier makes more sense than * because we only want to match those numbers that we want to modify, those that already have 5 digits after the decimal separator do not have to be matched.
I have developed the following regexp to capture float numbers.
([+-]?[0-9]+\.?[0-9]+([eE][-+]?[0-9]+)?)
It works fine for such things as 4.08955e-11 or 3.57. Now by stupid chance my parser came across 0 and failed. I guess I need to make all following the decimal point optional. But how do I do that?
Contrary to what one might think, matching every possible form of floating point number (including NaN etc) with a manageable regular expression that still discards e.g. impossibly large numbers or pseudo-octals is non-trivial.
There are some ideas about reducing the risk of false positives by using word boundaries, but note that those match boundaries between word characters (usually alphanumerics and underscore).
The scan command allows simple and reliable validation and extraction of floating point numbers:
scan $number %f
If you make all following the decimal point optional (which itself is optional) you could match values like 2.
Note that your regex does not match a single digit because you match 2 times one or more digits [0-9]+
If you only want to match float numbers or zero you could use an alternation and for example use word boundaries \b:
\b[-+]?(?:[0-9]+\.[0-9]+(?:[eE][-+]?[0-9]+)?|0)\b
Explanation
[-+]? Match optional + or -
\b Word boundary
(?: Non capturing group
[0-9]+\.[0-9]+ match one or more digits dot and one or more digits
(?:[eE][-+]?[0-9]+)? Optional exponent part
| Or
0 Match literally
) Close non capturing group
\b Word boundary
To match a float value that does not start with a dot and could be one or more digits without a dot you cold use use:
^[-+]?[0-9]+(?:\.[0-9]+)?(?:[eE][-+]?[0-9]+)?$
Perhaps using alternatives:
{[-+]?(?:\y[0-9]+(?:\.[0-9]*)?|\.[0-9]+\y)(?:[eE][-+]?[0-9]+\y)?}
I need a regular expression that validates a number, but doesn't require a digit after the decimal.
ie.
123
123.
123.4
would all be valid
123..
would be invalid
Any would be greatly appreciated!
Use the following:
/^\d*\.?\d*$/
^ - Beginning of the line;
\d* - 0 or more digits;
\.? - An optional dot (escaped, because in regex, . is a special character);
\d* - 0 or more digits (the decimal part);
$ - End of the line.
This allows for .5 decimal rather than requiring the leading zero, such as 0.5
/\d+\.?\d*/
One or more digits (\d+), optional period (\.?), zero or more digits (\d*).
Depending on your usage or regex engine you may need to add start/end line anchors:
/^\d+\.?\d*$/
Debuggex Demo
You need a regular expression like the following to do it properly:
/^[+-]?((\d+(\.\d*)?)|(\.\d+))$/
The same expression with whitespace, using the extended modifier (as supported by Perl):
/^ [+-]? ( (\d+ (\.\d*)?) | (\.\d+) ) $/x
or with comments:
/^ # Beginning of string
[+-]? # Optional plus or minus character
( # Followed by either:
( # Start of first option
\d+ # One or more digits
(\.\d*)? # Optionally followed by: one decimal point and zero or more digits
) # End of first option
| # or
(\.\d+) # One decimal point followed by one or more digits
) # End of grouping of the OR options
$ # End of string (i.e. no extra characters remaining)
/x # Extended modifier (allows whitespace & comments in regular expression)
For example, it will match:
123
23.45
34.
.45
-123
-273.15
-42.
-.45
+516
+9.8
+2.
+.5
And will reject these non-numbers:
. (single decimal point)
-. (negative decimal point)
+. (plus decimal point)
(empty string)
The simpler solutions can incorrectly reject valid numbers or match these non-numbers.
this matches all requirements:
^\d+(\.\d+)?$
Try this regex:
\d+\.?\d*
\d+ digits before optional decimal
.? optional decimal(optional due to the ? quantifier)
\d* optional digits after decimal
I ended up using the following:
^\d*\.?\d+$
This makes the following invalid:
.
3.
This is what I did. It's more strict than any of the above (and more correct than some):
^0$|^[1-9]\d*$|^\.\d+$|^0\.\d*$|^[1-9]\d*\.\d*$
Strings that passes:
0
0.
1
123
123.
123.4
.0
.0123
.123
0.123
1.234
12.34
Strings that fails:
.
00000
01
.0.
..
00.123
02.134
you can use this:
^\d+(\.\d)?\d*$
matches:
11
11.1
0.2
does not match:
.2
2.
2.6.9
^[+-]?(([1-9][0-9]*)?[0-9](\.[0-9]*)?|\.[0-9]+)$
should reflect what people usually think of as a well formed decimal number.
The digits before the decimal point can be either a single digit, in which case it can be from 0 to 9, or more than one digits, in which case it cannot start with a 0.
If there are any digits present before the decimal sign, then the decimal and the digits following it are optional. Otherwise, a decimal has to be present followed by at least one digit. Note that multiple trailing 0's are allowed after the decimal point.
grep -E '^[+-]?(([1-9][0-9]*)?[0-9](\.[0-9]*)?|\.[0-9]+)$'
correctly matches the following:
9
0
10
10.
0.
0.0
0.100
0.10
0.01
10.0
10.10
.0
.1
.00
.100
.001
as well as their signed equivalents, whereas it rejects the following:
.
00
01
00.0
01.3
and their signed equivalents, as well as the empty string.
What language? In Perl style: ^\d+(\.\d*)?$
What you asked is already answered so this is just an additional info for those who want only 2 decimal digits if optional decimal point is entered:
^\d+(\.\d{2})?$
^ : start of the string
\d : a digit (equal to [0-9])
+ : one and unlimited times
Capturing Group (.\d{2})?
? : zero and one times
. : character .
\d : a digit (equal to [0-9])
{2} : exactly 2 times
$ : end of the string
1 : match
123 : match
123.00 : match
123. : no match
123.. : no match
123.0 : no match
123.000 : no match
123.00.00 : no match
try this. ^[0-9]\d{0,9}(\.\d{1,3})?%?$ it is tested and worked for me.
Regular expression:
^\d+((.)|(.\d{0,1})?)$
use \d+ instead of \d{0,1} if you want to allow more then one number use \d{0,2} instead of \d{0,1} if you want to allow up to two numbers after coma. See the example below for reference:
or
^\d+((.)|(.\d{0,2})?)$
or
^\d+((.)|(.\d+)?)$
Explanation
(These are generated by regex101)
^ asserts position at start of a line
\d matches a digit (equivalent to [0-9])
+ matches the previous token between one and unlimited times, as many times as possible, giving back as needed (greedy)
1st Capturing Group ((.)|(.\d{0,1})?)
1st Alternative (.)
2nd Capturing Group (.)
. matches any character (except for line terminators)
2nd Alternative (.\d{0,1})?
3rd Capturing Group (.\d{0,1})?
? matches the previous token between zero and one times, as many times as possible, giving back as needed (greedy)
. matches any character (except for line terminators)
\d matches a digit (equivalent to [0-9])
{0,1} matches the previous token between zero and one times, as many times as possible, giving back as needed (greedy)
$ asserts position at the end of a line
Sandbox
Play with regex here: https://regex101.com/
(?<![^d])\d+(?:\.\d+)?(?![^d])
clean and simple.
This uses Suffix and Prefix, RegEx features.
It directly returns true - false for IsMatch condition
^\d+(()|(\.\d+)?)$
Came up with this. Allows both integer and decimal, but forces a complete decimal (leading and trailing numbers) if you decide to enter a decimal.
In Perl, use Regexp::Common which will allow you to assemble a finely-tuned regular expression for your particular number format. If you are not using Perl, the generated regular expression can still typically be used by other languages.
Printing the result of generating the example regular expressions in Regexp::Common::Number:
$ perl -MRegexp::Common=number -E 'say $RE{num}{int}'
(?:(?:[-+]?)(?:[0123456789]+))
$ perl -MRegexp::Common=number -E 'say $RE{num}{real}'
(?:(?i)(?:[-+]?)(?:(?=[.]?[0123456789])(?:[0123456789]*)(?:(?:[.])(?:[0123456789]{0,}))?)(?:(?:[E])(?:(?:[-+]?)(?:[0123456789]+))|))
$ perl -MRegexp::Common=number -E 'say $RE{num}{real}{-base=>16}'
(?:(?i)(?:[-+]?)(?:(?=[.]?[0123456789ABCDEF])(?:[0123456789ABCDEF]*)(?:(?:[.])(?:[0123456789ABCDEF]{0,}))?)(?:(?:[G])(?:(?:[-+]?)(?:[0123456789ABCDEF]+))|))
For those who wanna match the same thing as JavaScript does:
[-+]?(\d+\.?\d*|\.\d+)
Matches:
1
+1
-1
0.1
-1.
.1
+.1
Drawing: https://regexper.com/#%5B-%2B%5D%3F%28%5Cd%2B%5C.%3F%5Cd*%7C%5C.%5Cd%2B%29
what the regular expression of a line of string containing ONLY float numbers separated with spaces or tabs. The float number can be negative, like -999.999
(?:-?(?:\d+(?:\.\d*)|.\d+)[ \t]*)+
is one possibility. In more readable format:
(?:
-? # Optional negative sign
(?:
\d+(?:\.\d*) # Either an integer part with optional decimal part
|
.\d+ # Or a decimal part that starts with a period
)
[ \t]* # Followed by any number of tabs or spaces
)+ # One or more times
Let's come up with a regex for a float, and then see what we can do about the rest.
A float is:
An optional negative sign
Followed by a number of digits
Followed by an optional decimal point and then more digits
Followed be "e"
Followed by a number of digits (with an optional sign).
Put that together, and we get:
/-?[0-9]+(\.[0-9]+)?([Ee][+-]?[0-9]+)?/
Now, this is pretty loose, but you can tweak it if you want to tighten it up a little. Now, for any number of these with spaces in between, it's pretty trivial:
/^(F\s+)+$/
Put it all together, we end up with:
/^(-?[0-9]+(\.[0-9]+)?([Ee][+-]?[0-9]+)?\s+)+$/
A regex for a float would look like this: -?\d+\.?\d+
A whitespace separator looks like this: \s
Put them together, allow it to repeat, make sure the end has a float (not a separator):
((-?\d+\.?\d*)\s)*(-?\d+\.?\d*))
The escaping and \d vs [0-9] might change, depending on your flavor of regex.