How to implement negative null floating point regex - javascript

What could I use as a regex that validates floating point numbers?
The numbers could be negative and cannot start by 0 (unless it is a real 0, or a 0 followed by decimal points such in 0.00123)
On the other hand, the regex should validate partial/uncompleted numbers. For example:
''(empty string) is valid
- is valid (it could still be a valid number with appropiate user input)
0 is valid (as 0.1 could be valid)
0.0 is valid, for the same previous reason
-0.000 is valid
-01 is not valid
.2 is not valid
I tried with this regexp:
^(-|-?0\.?[0-9]*|-?[1-9][0-9]*\.?[0-9]*|)$
It allows me to discard all non-numeric values ​​and still allow fractional values, but how to implement the possibility that the user cannot enter numbers in the format -0232, but can enter -0.232

If I get it right, you want a regex that validates "could-be-a-future-decimal" numbers. So (empty string) -, -0, 0 and 0.... should be valid.
You may use this regex:
^-?(?:(?!0\d)\d+\.?\d*)?$
You have a demo here.
Explained:
^ # Start of string
-? # an optional minus
(?: # non-capturing group
(?!0\d) # whatever comes next, it cannot be 0 + number
\d+ # at least one number
\.? # optional dot
\d* # any quantity of numbers
)? # previous group is optional
$
Also, bear in mind that the previous thing is a regex object. So you should try to use it like this in javascript:
/myregex/
However, javascript also allows using string-based regexes (which by your comments it seems that is what you want...)
In that case you can use the new RegExp syntax. However, bear in mind that since you'll be using a string, you need to scape any characters a string should scape.
For example, you may use:
function return_some_regex_string() {
// NOTE backslashes had to be scaped since this is a string,
// not a real regex object
return '^-?(?:(?!0\\d)\\d+\\.?\\d*)?$';
}
console.log("0.123".match(new RegExp(return_some_regex_string())));
console.log("-03233".match(new RegExp(return_some_regex_string())));
console.log("333-3334".match(new RegExp(return_some_regex_string())));
But, at the same time, you could already return the regex object, which seems a simpler way to work than using string-based regexes:
function return_some_regex() {
return /^-?(?:(?!0\d)\d+\.?\d*)?$/;
}
console.log("0.123".match(return_some_regex()));
console.log("-03233".match(return_some_regex()));
console.log("333-3334".match(return_some_regex()));

You could match an optional hyphen, followed by either a floating point or match a number starting with 1-9
^-?(?:\d+\.\d+|[1-9]\d*|0\.?)$
The pattern will match
^ Start of string
-? Optional hyphen
(?: Non capture group
\d+\.\d+ Match 1+ digits, dot and 1+ digits
| Or
[1-9]\d* Match a digit 1-9 and 0+ digits 0-9
| Or
0\.? Match a zero and optional dot
) Close group
$ End of string
Regex demo
let pattern = /^-?(?:\d+\.\d+|[1-9]\d*|0\.?)$/;
["-0123",
"0123123",
"-03123123",
"1234",
"-0.234",
"0.4",
"0.3312312",
"1",
"-1234",
"0",
"0."
].forEach(s => console.log(`${s} match:${pattern.test(s)}`));
If you want to match an empty string or just a single hyphen, you can also make the second part optional.
^-?(?:\d+\.\d+|[1-9]\d*|0\.?)?$
Regex demo

Related

Limit 10 characters is numbers and only 1 dot

I'm having a regex problem when input
That's the requirement: limit 10 characters (numbers) including dots, and only 1 dot is allowed
My current code is only 10 characters before and after the dot.
^[0-9]{1,10}\.?[0-9]{0,10}$
thank for support.
You could assert 10 chars in the string being either . or a digit.
Then you can match optional digits, and optionally match a dot and again optional digits:
^(?=[.\d]{10}$)\d*(?:\.\d*)?$
The pattern matches:
^ Start of string
(?=[.\d]{10}$) Positive lookahead, assert 10 chars . or digit till the end of string
\d* Match optional digits
(?:\.\d*)? Optionally match a `. and optional digits
$ End of string
See a regex demo.
If the pattern should not end on a dot:
^(?=[.\d]{10}$)\d*(?:\.\d+)?$
Regex demo
The decimal point throws a wrench into most single pattern approaches. I would probably use an alternation here:
^(?:\d{1,10}|(?=\d*\.)(?!\d*\.\d*\.)[0-9.]{2,11})$
This pattern says to match:
^ from the start of the number
(?:
\d{1,10} a pure 1 to 10 digit integer
| OR
(?=\d*\.) assert that one dot is present
(?!\d*\.\d*\.) assert that ONLY one dot is present
[0-9.]{2,11} match a 1 to 10 digit float
)
$ end of the number
You can use a lookahead to achieve your goals.
First, looking at your regex, you've used [0-9] to represent all digit characters. We can shorten this to \d, which means the same thing.
Then, we can focus on the requirement that there be only one dot. We can test for this with the following pattern:
^\d*\.?\d*$
\d* means any number of digit characters
\.? matches one literal dot, optionally
\d* matches any number of digit characters after the dot
$ anchors this to the end of the string, so the match can't just end before the second dot, it actually has to fail if there's a second dot
Now, we don't actually want to consume all the characters involved in this match, because then we wouldn't be able to ensure that there are <=10 characters. Here's where the lookahead comes in: We can use the lookahead to ensure that our pattern above matches, but not actually perform the match. This way we verify that there is only one dot, but we haven't actually consumed any of the input characters yet. A lookahead would look like this:
^(?=\d*\.?\d*$)
Next, we can ensure that there are aren't more than 10 characters total. Since we already made sure there are only dots and digits with the above pattern, we can just match up to 10 of any characters for simplicity, like so:
^.{1,10}$
Putting these two patterns together, we get this:
^(?=\d*\.?\d*$).{1,10}$
This will only match number inputs which have 10 or fewer characters and have no more than one dot.
If you would like to ensure that, when there is a dot, there is also a digit accompanying it, we can achieve this by adding another lookahead. The only case that meets this condition is when the input string is just a dot (.), so we can just explicitly rule this case out with a negative lookahead like so:
(?!\.$)
Adding this back in to our main expression, we get:
^(?=\d*\.?\d*$)(?!\.$).{1,10}$

RegEx - contain specific number after period

I have strings similar to; First Half Goals 1.5/2. The text at the beginning can be anything so cannot depend on that as part of the RegEx as it varies. What I want to do is to match the number sequence at the end. Test samples would be:
5
0.5/1
2.5/3
147
The sequence can either contain a slash or it cannot. If it contains a slash, there must be a value after it - either integer or decimal. The value before the decimal (if one is present) can be any number of digits - \d+ - but the value after should must be 0 or 5 - (0|5). The 1st value before the slash (/) is either an integer or a decimal. If the sequence contains a slash, then the number after is also either a integer or a decimal. All values are positive.
The main point of this RegEx is that I need it to only match once. The was the RegEx I was using:
(\d(\.(0|5))?\/\d(\.(0|5)))|(\d\.(0|5))|(\d)
The issue with the regex above is that the example string I use; First Half Goals 1.5/2 matched twice. Once on the 1.5 & the 2nd on the 2. I have now altered it to be this:
\d+(\.(0|5))?(\/?\d+(\.(0|5))?)?
This is slightly better but if I give the test sample; 1.6/2, it will match 6/2. This will be because the decimal section on the former number is optional. I'm not sure if a lookbehind would come in handy here, I don't have much experience with them. Sadly the text beforehand is so unpredictable otherwise I could trim the string to only get the wanted substring & then match from the start of the string but can't do that. An outline of what should match & what should not:
1 // Match
5.5 // Match
7.8 // No Match
0/0.5 // Match
147/147.5 // Match
2. // No Match
6.5/ // No Match
7.0/8 // Match
10.0 // Match
1./2.5 // No Match
5./6 // No Match
/157 // No Match
/4.5 // No Match
I've tried to explain as best I could but if you need more details then I'll provide them
In Node.js, RegExp supports lookbehinds and you may use
/\b(?<!\d\.|\/)\d+(?:\.[05])?(?:\/\d+(?:\.[05])?)?$/
See the regex demo
Details
\b - word boundary
(?<!\d\.|\/) - no digit + dot or a slash are allowed immediately to the left of the current location
\d+ - one or more digits
(?:\.[05])? - an optional sequence of
\. - a dot
[05] - 0 or 5
(?:\/\d+(?:\.[05])?)? - an optional sequence of
\/\d+ - a / and 1+ digits
(?:\.[05])? - an optional sequence of a dot and then 0 or 5
$ - end of string.

Javascript Regular Expresion [duplicate]

I'm trying to write a RegExp to match only 8 digits, with one optional comma maybe hidden in-between the digits.
All of these should match:
12345678
12,45678
123456,8
Right now I have:
^[0-9,]{8}
but of course that erroneously matches 012,,,67
Example:
https://regex101.com/r/dX9aS9/1
I know optionals exist but don't understand how to keep the 8 digit length applying to the comma while also keeping the comma limited to 1.
Any tips would be appreciated, thanks!
To match 8 char string that can only contain digits and an optional comma in-between, you may use
^(?=.{8}$)\d+,?\d+$
See the regex demo
The lookahead will require the string to contain 8 chars. ,? will make matching a comma optional, and the + after \d will require at least 1 digit before and after an optional comma.
If you need to match a string that has 8 digits and an optional comma, you can use
^(?:(?=.{9}$)\d+,\d+|\d{8})$
See the regex demo
Actually, the string will have 9 characters in the string (if it has a comma), or just 8 - if there are only digits.
Explanation:
^ - start of string
(?:(?=.{9}$)\d+,\d+|\d{8}) - 2 alternatives:
(?=.{9}$)\d+,\d+ - 1+ digits followed with 1 comma followed with 1+ digits, and the whole string matched should be 9 char long (8 digits and 1 comma)
| - or
\d{8} - 8 digits
$ - end of string
See the Java code demo (note that with String#matches(), the ^ and $ anchors at the start and end of the pattern are redundant and can be omitted since the pattern is anchored by default when used with this method):
List<String> strs = Arrays.asList("0123,,678", "0123456", // bad
"01234,567", "01234567" // good
);
for (String str : strs)
System.out.println(str.matches("(?:(?=.{9}$)\\d+,\\d+|\\d{8})"));
NOTE FOR LEADING/TRAILING COMMAS:
You just need to replace + (match 1 or more occurrences) quantifiers to * (match 0 or more occurrences) in the first alternative branch to allow leading/trailing commas:
^(?:(?=.{9}$)\d*,\d*|\d{8})$
See this regex demo
You can use following regex if you want to let trailing comma:
^((\d,?){8})$
Demo
Otherwise use following one:
^((\d,?){8})(?<!,)$
Demo
(?<!,) is a negative-lookbehind.
/^(?!\d{0,6},\d{0,6},\d{0,6})(?=\d[\d,]{6}\d).{8}$/
I guess this cooperation of positive and negative look-ahead does just what's asked. If you remove the start and end delimiters and set the g flag then it will try to match the pattern along decimal strings longer than 8 characters as well.
Please try http://regexr.com/3d63m
Explanation: The negative look ahead (?!\d{0,6},\d{0,6},\d{0,6}) tries not to find any commas side by side if they have 6 or less decimal characters in between while the positive look ahead (?=\d[\d,]{6}\d) tries to find 6 decimal or comma characters in between two decimal characters. And the last .{8} selects 8 characters.

Regular expression for thousand separator without decimal point

I have regular expression which is working fine for decimal .
In below expression I want to remove decimal match to validate Integer
^-?(\d*)(,\d{1,3})*(?:[,]|([.]\d{0,2}))?$
Valid Match : 1,000 & 111, & -1000,00
Invalid Match : -,1 & 1,,,,
Use
^-?(?:\d{1,3}(?:,\d{3})*|\d+)?,?$
|---1---|
|---2-----|
|-3-|
The ^ asserts the position at the start of the string and $ asserts the position at the string end. -? matches an optional -. Part 1 matches 1 to 3 digits, Part 2 matches 0+ sequences of , followed with 3 digits, part 3 is an alternative to Part 1 & 2 combined and matches just 1+ digits. The whole number pattern is optional since the (?:...|...) is quantified with ? - one or zero occurrences.
The ,? before $ allows an optional comma at the end, delete if not required (you mentioned you need to allow 100, in the comments).
See the regex demo

regex allows one character (it should not) why?

Hello I am trying to create a regex that recognizes money and numbers being inputted. I have to allow numbers because I am expecting non-formatted numbers to be inputted programmatically and then I will format them myself. For some reason my regex is allowing a one letter character as a possible input.
[\$]?[0-9,]*\.[0-9][0-9]
I understand that my regex accepts the case where multiple commas are added and also needs two digit after the decimal point. I have had an idea of how to fix that already. I have narrowed it down to possibly the *\. as the problem
EDIT
I found the regex expression that worked [\$]?([0-9,])*[\.][0-9]{2} but I still don't know how or why it was failing in the first place
I am using the .formatCurrency() to format the input into a money format. It can be found here but it still allows me to use alpha characters so i have to further masked it using the $(this).inputmask('Regex', { regex: "[\$]?([0-9,])*[\.][0-9]{2}" }); where input mask is found here and $(this) is a reference to a input element of type text. My code would look something like this
<input type="text" id="123" data-Money="true">
//in the script
.find("input").each(function () {
if ($(this).attr("data-Money") == "true") {
$(this).inputmask('Regex', { regex: "[\$]?([0-9,])*[\.][0-9]{2}" });
$(this).on("blur", function () {
$(this).formatCurrency();
});
I hope this helps. I try creating a JSfiddle but Idk how to add external libraries/plugin/extension
The "regular expression" you're using in your example script isn't a RegExp:
$(this).inputmask('Regex', { regex: "[\$]?([0-9,])*[\.][0-9]{2}" });
Rather, it's a String which contains a pattern which at some point is being converted into a true RegExp by your library using something along the lines of
var RE=!(value instanceof RegExp) ? new RegExp(value) : value;
Within Strings a backslash \ is used to represent special characters, like \n to represent a new-line. Adding a backslash to the beginning of a period, i.e. \., does nothing as there is no need to "escape" the period.
Thus, the RegExp being created from your String isn't seeing the backslash at all.
Instead of providing a String as your regular expression, use JavaScript's literal regular expression delimiters.
So rather than:
$(this).inputmask('Regex', { regex: "[\$]?([0-9,])*[\.][0-9]{2}" });
use
$(this).inputmask('Regex', { regex: /[\$]?([0-9,])*[\.][0-9]{2}/ });
And I believe your "regular expression" will perform as you expect.
(Note the use of forward slashes / to delimit your pattern, which JavaScript will use to provide a true RegExp.)
Firstly, you can replace '[0-9]' with '\d'. So we can rewrite your first regex a little more cleanly as
\$?[\d,]*\.\d\d
Breaking this down:
\$? - A literal dollar sign, zero or one
[\d,]* - Either a digit or a comma, zero or more
\. - A literal dot, required
\d - A digit, required
\d - A digit, required
From this, we can see that the minimum legal string is \.\d\d, three characters long. The regex you gave will never validate against any one character string.
Looking at your second regex,
[\$]? - A literal dollar sign, zero or one
([0-9,])* - Either a digit or a comma, subexpression for later use, zero or more
[\.] - A literal dot, required
[0-9]{2} - A digit, twice required
This has the exact same minimum matchable string as above - \.\d\d.
edit: As mentioned, depending on the language you may need to escape forward slashes to ensure they aren't misinterpretted by the language when processing the string.
Also, as an aside, the below regex is probably closer to what you need.
[A-Z]{3} ?(\d{0,3}(?:([,. ])\d{3}(?:\2\d{3})*)?)(?!\2)[,.](\d\d)\b
Explanation:
[A-Z]{3} - Three letters; for an ISO currency code
? - A space, zero or more; for readability
( - Capture block; to catch the integer currency amount
\d{0,3} - A digit, between one and three; for the first digit block
(?: - Non capturing block (NC)
([,. ]) - A comma, dot or space; as a thousands delimiter
\d{3} - A digit, three; the first possible whole thousands
(?: - Non capturing block (NC)
\2 - Match 2; the captured thousands delimiter above
\d{3} - A digits, three
)* - The above group, zero or more, i.e. as many thousands as we want
)? - The above (NC) group, zero or one, ie. all whole thousands
) - The above group, i.e everything before the decimal
[.,] - A comma or dot, as a decimal delimiter
(\d{2}) - Capture, A digit, two; ie. the decimal portion
\b - A word boundry; to ensure that we don't catch another
digit in the wrong place.
The negative lookahead was provided by an answer from John Kugelman in this question.
This correctly matches (matches enclosed in square brackets):
[AUD 1.00]
[USD 1,300,000.00]
[YEN 200 000.00]
I need [USD 1,000,000.00], all in non-sequential bills.
But not:
GBP 1.000
YEN 200,000

Categories

Resources