Regex - checking range of numbers with constraints - javascript

In javascript I need to parse a user input. The input is of the format: number - number. This can be repeated and separated by commas.
The following are some examples:
1-10
4-10,13-17
6-10,3-8,4-12
Here is the regex I wrote for this
(\d+[-]\d+[,]?)
However, there are 2 constraints.
The first number must be less than the second number ( 4-5 is valid but 5-4 is not)
Every number must be between 1 and N (inclusive). I will specify N.
Is there a way I can enforce these constraints with regex?

While you can certainly match the format with regexes, you can't do the kind of verification you want with them. What I would recommend is something like this (in JS):
function verifyList(list) {
var matches = list.match(/\d+-\d+/g);
for (match in matches) {
var numbers = match.match(/(\d+)-(\d+)/);
if (numbers[1] >= numbers[2]) return false;
}
return true;
}

Related

How to solve Invalid regular expression error when running app in android in React Native? [duplicate]

When a decimal point is not allowed at the beginning or end, it can be in the middle and there must be only one decimal point.
I used regular expressions to create the expression I wanted. Numbers must be entered, but no English characters or other string values ​​can be used. Only one decimal point can be used, but I do not want to allow a decimal point at the beginning. But the last one is allowed to be inserted. One decimal point in the middle of a number with a trailing decimal point is allowed. In addition, even if there is no decimal point in the middle of a number, it is allowed to have a decimal point at the end.
like this
(o )13.4. 13.
(x) .
However, when using my regular expression, the decimal point is used more than once, and the decimal point is also used at the beginning and end.
this is my regex
let regex = /[^\d.]/g;
how can i fix this?
const str = '123.12';
const regex = new RegExp('^\\d+([.]\\d+)?$');
console.log(regex.test(str));
in my. you might find a way to fix your problem ,but regex sometimes is not the best solution. if possible ,just write a method to limit ,
1.only numbers or point are allowed
2.point is only one time in the string, but not begin or the end
it's might not hard for you, and trust me it would be fast.
here are some exp:
if(str[0]==='.' || str[str.length -1] === '.'){
return false;
}
let pointCount = 0;
for(let i=0; i<str.length; i++){
let uniCode = str.charCodeAt(i);
// .
if(uniCode === 46){
pointCount ++;
}
// number and point
if(uniCode < 48 && uniCode > 57 && uniCode !== 46){
return false;
}
}
if(pointCount > 1){
return false;
}
Let me summarize your requirements as your message and provided expression is not easy to understand or conflicting with each other.
allowed are values, which…
in general contain nothing else than numbers and maybe dots
just number(s), eg. 1 or 12
just number(s) with trailing dot, eg. 1. or 12.
number(s) with decimal(s), eg. 1.2 or 12.3 or 12.34
number(s) with decimal(s) and trailing dot, eg. 1.2. or 12.3. or 12.34.
disallowed are values, which…
are empty
start with a dot
contain repeating dots
contain more than two dots
If that is correct, then I would go with the following expression.
/^\d+(?:\.\d+)?\.?$/
Have you considered that negative numbers could appear in you data? If that is the case then take the following expression
/^-?\d+(?:\.\d+)?\.?$/
You can remove the ?: parts if you do not mind about the capturing groups and look for better readability.

How can I find the missing integer in a string of random non-repeating integers in a range

I'm having trouble even beginning to think of how to do this.
I need to find a missing number in a string of random numbers that don't have separators.
Here's an example: 14036587109. In this example, the missing number is 2, the range is 0-10 (inclusive).
How can I write a JavaScript/Node.JS program to solve this?
The part I can't figure out is how the program separates the numbers; In the example above, how would the program know that the number 10 (before the last number) isn't the numbers 1 and 0.
There are two things we know about the missing integer: the total number of digits in the input tells us the number of digits in the missing integer, and (as #samgak mentioned in a comment) counting the occurrences of each digit in the input tells us which digits the missing integer is made of. This may give us a quick path to the solution, if one of the permutations of those digits is missing from the input. If it doesn't, then:
Find the integers from highest to lowest number of digits; if the range is e.g. 0-999, then search the 3-digit integers first, then 2, then 1.
If an integer is only present at one location in the input, mark it as found, and remove it from the input.
Then, start again with the longest integers that haven't been found yet, and look at the ones that are present at two locations; try both options, and then check whether all other integers that rely on the digits we're using are also present; e.g. if 357 is present at two locations:
... 1235789 ... 2435768 ...
357 357
23 43
123 243
235 435
578 576
78 76
789 768
When trying the first location for the 357, check whether there is another possibility for 23, 123, 235, 578, 78, and 789. For the second location, check 43, 243, 435, 576, 76 and 768.
If these checks show that only one of the options is possible, mark the number as found and remove it from the input.
Go on to do this for shorter integers, and for integers that are present at 3, 4, ... locations. If, after doing this to a certain point, there is still no result, you may have to recursively try several options, which will quickly lead to a huge number of options. (With especially crafted large input, it is probably possible to thwart this method and make it unusably slow.) But the average complexity with random input may be decent.
Actually, when you find an integer that is only present in one location in the input, but it is a permutation of the missing digits, you should not remove it, because it could be the missing integer. So the algorithm might be: remove all integers you can unequivocally locate in the input, then try removing all possible missing integers one by one, and look for inconsistencies, i.e. other missing numbers that don't have the correct length or digits.
It's all a question of heuristics, of course. You try something simple, if that doesn't work you try something more complicated, if that doesn't work, you try something even more complicated... and at each step there are several options, and each one could be optimal for some input strings but not for others.
E.g. if the range is 0-5000, you'd start by marking the 4-digit integers that are only present at one location. But after that, you could do the same thing again (because integers that were present twice could have had one of their options removed) until there's no more improvement, or you could check integers that are present twice, or integers that are present up to x times, or move on to 3-digit integers... I don't think there's a straightforward way to know which of these options will give the best result.
This solution should work for any input string and any start/end range:
We can think about the numbers in the string as a pool of digits that we can choose from. We start at startRange and go through to endRange, looking for each number along the way in our pool of digits.
When we find a number that can be composed from our pool of digits, we delete those digits from our pool of digits, as those digits are already being used to form a number in our range.
As soon as we come across a number that cannot be composed from our pool of digits, that must be the missing number.
const str = "14036587109"; // input
const numsLeft = str.split("").map(num => parseInt(num)); // array of numbers
const startRange = 0;
const endRange = 10;
for(let i = startRange; i <= endRange ; i++) {
// check if number can be formed given the numbers left in numsLeft
const numFound = findNum(numsLeft, i);
if(!numFound) {
console.log("MISSING: " + i); // prints 2
break;
}
}
function findNum(numsLeft, i) {
// array of digits
const numsToFind = String(i).split("").map(num => parseInt(num));
// default is true, if all digits are found in numsLeft
let found = true;
numsToFind.forEach(num => {
// find digit in numsLeft
const numFoundIndex = numsLeft.indexOf(num);
if(numFoundIndex < 0) {
// digit was not found in numsLeft
found = false;
return;
} else {
// digit was found; delete digit from numsLeft
numsLeft.splice(numFoundIndex, 1);
}
});
return found;
}
var input = '10436587109';
var range = [10,9,8,7,6,5,4,3,2,1,0];
var expr1 = new RegExp(range.join('|'),'g');
var expr2 = new RegExp('[0-9]','g');
var a = input.match(expr1).map(Number).concat(input.match(expr2).map(Number));
var x = range.filter(function(i){ return a.indexOf(i)===-1; });

Convert number from input with/without separators to whole number

I am trying to allow only [0-9], ., and , from being input, as I only want the user to be able to input numbers with and without separators. These are the tests I want to allow:
1000 == 1.000,00
1000 == 1,000.00
1000 == 1.000
1000 == 1,000
1000 == 1000
How users type these is completely up to them and what they usually do. I want to check if what the user inputs is equal to 1000.
https://jsfiddle.net/m2pq06b1/
What is the best way to do this? I need all cases above to be true.
From the comments:
It's common that if you type three numbers after a separator, it means
thousand. It's quite common. When you only type two, it generally
means cents or hundredths of a whole (basically 1-99).
Based on that, I propose the following solution:
Replace all separators (, and .) followed by exactly two digits and the end of the string with a decimal point.
Remove all separators not followed by exactly two digits and the end of the string.
Parse the sanitized value to a number using parseFloat().
var values = ["1.000,00", "1,000.00", "1.000", "1,000","1000"];
values.forEach(value => {
let sanitized = value.replace(/[.,](?=\d{2}$)/g, '.').replace(/[.,](?!\d{2}$)/g, '');
let number = parseFloat(sanitized);
console.log(number == 1000);
});

RegEx to filter out all but one decimal point [duplicate]

i need a regular expression for decimal/float numbers like 12 12.2 1236.32 123.333 and +12.00 or -12.00 or ...123.123... for using in javascript and jQuery.
Thank you.
Optionally match a + or - at the beginning, followed by one or more decimal digits, optional followed by a decimal point and one or more decimal digits util the end of the string:
/^[+-]?\d+(\.\d+)?$/
RegexPal
The right expression should be as followed:
[+-]?([0-9]*[.])?[0-9]+
this apply for:
+1
+1.
+.1
+0.1
1
1.
.1
0.1
Here is Python example:
import re
#print if found
print(bool(re.search(r'[+-]?([0-9]*[.])?[0-9]+', '1.0')))
#print result
print(re.search(r'[+-]?([0-9]*[.])?[0-9]+', '1.0').group(0))
Output:
True
1.0
If you are using mac, you can test on command line:
python -c "import re; print(bool(re.search(r'[+-]?([0-9]*[.])?[0-9]+', '1.0')))"
python -c "import re; print(re.search(r'[+-]?([0-9]*[.])?[0-9]+', '1.0').group(0))"
You can check for text validation and also only one decimal point validation using isNaN
var val = $('#textbox').val();
var floatValues = /[+-]?([0-9]*[.])?[0-9]+/;
if (val.match(floatValues) && !isNaN(val)) {
// your function
}
This is an old post but it was the top search result for "regular expression for floating point" or something like that and doesn't quite answer _my_ question. Since I worked it out I will share my result so the next person who comes across this thread doesn't have to work it out for themselves.
All of the answers thus far accept a leading 0 on numbers with two (or more) digits on the left of the decimal point (e.g. 0123 instead of just 123) This isn't really valid and in some contexts is used to indicate the number is in octal (base-8) rather than the regular decimal (base-10) format.
Also these expressions accept a decimal with no leading zero (.14 instead of 0.14) or without a trailing fractional part (3. instead of 3.0). That is valid in some programing contexts (including JavaScript) but I want to disallow them (because for my purposes those are more likely to be an error than intentional).
Ignoring "scientific notation" like 1.234E7, here is an expression that meets my criteria:
/^((-)?(0|([1-9][0-9]*))(\.[0-9]+)?)$/
or if you really want to accept a leading +, then:
/^((\+|-)?(0|([1-9][0-9]*))(\.[0-9]+)?)$/
I believe that regular expression will perform a strict test for the typical integer or decimal-style floating point number.
When matched:
$1 contains the full number that matched
$2 contains the (possibly empty) leading sign (+/-)
$3 contains the value to the left of the decimal point
$5 contains the value to the right of the decimal point, including the leading .
By "strict" I mean that the number must be the only thing in the string you are testing.
If you want to extract just the float value out of a string that contains other content use this expression:
/((\b|\+|-)(0|([1-9][0-9]*))(\.[0-9]+)?)\b/
Which will find -3.14 in "negative pi is approximately -3.14." or in "(-3.14)" etc.
The numbered groups have the same meaning as above (except that $2 is now an empty string ("") when there is no leading sign, rather than null).
But be aware that it will also try to extract whatever numbers it can find. E.g., it will extract 127.0 from 127.0.0.1.
If you want something more sophisticated than that then I think you might want to look at lexical analysis instead of regular expressions. I'm guessing one could create a look-ahead-based expression that would recognize that "Pi is 3.14." contains a floating point number but Home is 127.0.0.1. does not, but it would be complex at best. If your pattern depends on the characters that come after it in non-trivial ways you're starting to venture outside of regular expressions' sweet-spot.
Paulpro and lbsweek answers led me to this:
re=/^[+-]?(?:\d*\.)?\d+$/;
>> /^[+-]?(?:\d*\.)?\d+$/
re.exec("1")
>> Array [ "1" ]
re.exec("1.5")
>> Array [ "1.5" ]
re.exec("-1")
>> Array [ "-1" ]
re.exec("-1.5")
>> Array [ "-1.5" ]
re.exec(".5")
>> Array [ ".5" ]
re.exec("")
>> null
re.exec("qsdq")
>> null
For anyone new:
I made a RegExp for the E scientific notation (without spaces).
const floatR = /^([+-]?(?:[0-9]+(?:\.[0-9]+)?|\.[0-9]+)(?:[eE][+-]?[0-9]+)?)$/;
let str = "-2.3E23";
let m = floatR.exec(str);
parseFloat(m[1]); //=> -2.3e+23
If you prefer to use Unicode numbers, you could replace all [0-9] by \d in the RegExp.
And possibly add the Unicode flag u at the end of the RegExp.
For a better understanding of the pattern see https://regexper.com/.
And for making RegExp, I can suggest https://regex101.com/.
EDIT: found another site for viewing RegExp in color: https://jex.im/regulex/.
EDIT 2: although op asks for RegExp specifically you can check a string in JS directly:
const isNum = (num)=>!Number.isNaN(Number(num));
isNum("123.12345678E+3");//=> true
isNum("80F");//=> false
converting the string to a number (or NaN) with Number()
then checking if it is NOT NaN with !Number.isNaN()
If you want it to work with e, use this expression:
[+-]?[0-9]+([.][0-9]+)?([eE][+-]?[0-9]+)?
Here is a JavaScript example:
var re = /^[+-]?[0-9]+([.][0-9]+)?([eE][+-]?[0-9]+)?$/;
console.log(re.test('1'));
console.log(re.test('1.5'));
console.log(re.test('-1'));
console.log(re.test('-1.5'));
console.log(re.test('1E-100'));
console.log(re.test('1E+100'));
console.log(re.test('.5'));
console.log(re.test('foo'));
Here is my js method , handling 0s at the head of string
1- ^0[0-9]+\.?[0-9]*$ : will find numbers starting with 0 and followed by numbers bigger than zero before the decimal seperator , mainly ".". I put this to distinguish strings containing numbers , for example, "0.111" from "01.111".
2- ([1-9]{1}[0-9]\.?[0-9]) : if there is string starting with 0 then the part which is bigger than 0 will be taken into account. parentheses are used here because I wanted to capture only parts conforming to regex.
3- ([0-9]\.?[0-9]): to capture only the decimal part of the string.
In Javascript , st.match(regex), will return array in which first element contains conformed part. I used this method in the input element's onChange event , by this if the user enters something that violates the regex than violating part is not shown in element's value at all but if there is a part that conforms to regex , then it stays in the element's value.
const floatRegexCheck = (st) => {
const regx1 = new RegExp("^0[0-9]+\\.?[0-9]*$"); // for finding numbers starting with 0
let regx2 = new RegExp("([1-9]{1}[0-9]*\\.?[0-9]*)"); //if regx1 matches then this will remove 0s at the head.
if (!st.match(regx1)) {
regx2 = new RegExp("([0-9]*\\.?[0-9]*)"); //if number does not contain 0 at the head of string then standard decimal formatting takes place
}
st = st.match(regx2);
if (st?.length > 0) {
st = st[0];
}
return st;
}
Here is a more rigorous answer
^[+-]?0(?![0-9]).[0-9]*(?![.])$|^[+-]?[1-9]{1}[0-9]*.[0-9]*$|^[+-]?.[0-9]+$
The following values will match (+- sign are also work)
.11234
0.1143424
11.21
1.
The following values will not match
00.1
1.0.00
12.2350.0.0.0.0.
.
....
How it works
The (?! regex) means NOT operation
let's break down the regex by | operator which is same as logical OR operator
^[+-]?0(?![0-9]).[0-9]*(?![.])$
This regex is to check the value starts from 0
First Check + and - sign with 0 or 1 time ^[+-]
Then check if it has leading zero 0
If it has,then the value next to it must not be zero because we don't want to see 00.123 (?![0-9])
Then check the dot exactly one time and check the fraction part with unlimited times of digits .[0-9]*
Last, if it has a dot follow by fraction part, we discard it.(?![.])$
Now see the second part
^[+-]?[1-9]{1}[0-9]*.[0-9]*$
^[+-]? same as above
If it starts from non zero, match the first digit exactly one time and unlimited time follow by it [1-9]{1}[0-9]* e.g. 12.3 , 1.2, 105.6
Match the dot one time and unlimited digit follow it .[0-9]*$
Now see the third part
^[+-]?.{1}[0-9]+$
This will check the value starts from . e.g. .12, .34565
^[+-]? same as above
Match dot one time and one or more digits follow by it .[0-9]+$

Regex for validating currency number format

I've got following formats, that are acceptable
1200000,00
1200000.00
1,200,000.00
1 200 000.00
1 200 000,00
1 200 000,0000
-1 200 000.00
At the moment I was able to verify only ^-?\\d+$, ^-?\\d+[\\,\\.]\\d{2}$, ^-?\\d+[\\,\\.]\\d{2,}$. Two last format are separate, so that I would know is rounding needed or not. All three format use gm flags to check string from start ^ to end $.
Those regular expressions cover only first two elements in list. Other elements, that use commas and spaces for thousand separation are not verified yet and I'm not sure how to achieve that.
Also there is a "beautifier" expression (\\d)(?=(\\d{3})+(?!\\d)), that will take this 1200000,00 and turn it into 1 200 000,00 with such usage '1200000,00'.replace(('(\\d)(?=(\\d{3})+(?!\\d))', 'g'), '$1 ').
So question states, what would be a correct regular expression to validate such format 1 200 000.00 or 1,200,000.00? Since I assume difference with \s\, could be easily done in same expression.
Thank you.
For validating the last two numbers, you can use the following:
^-?\d{1,3}(?:[\s,]\d{3})*(?:\.\d+)?$
1 2 3 4 5
Optional minus sign
1..3 digits
Zero or more fragments that consist of
comma or space
3 digits
optional fraction part consisting of a dot followed by 1 or more digits.
This doesn't directly solve the problem due to me misreading. But it might still be useful to someone so I'll let it stay.
Stop trying to solve every problem with regex. Regex is great when you have one or two very well defined strings. Not a million formats.
This can be solved with minimal regex. Magic is in the bold part.
var numbers = [
"1200000,00",
"1200000.00",
"1,200,000.00",
"1 200 000.00",
"1 200 000,00",
"1 200 000,0000",
"-1 200 000.00"
];
var parseWeirdNumber = function(numberString) {
//Split numbers to parts. , . and space are all valid delimiters.
var numberParts = numberString.split(/[.,\s]/);
//Remove the last part. **This means that all input must have fraction!!**
var fraction = numberParts.pop();
//Rejoin back without delimiters, and reapply the fraction.
//parseFloat to convert to a number
var number = parseFloat(numberParts.join('') + "." + fraction);
return number;
}
numbers = numbers.map(parseWeirdNumber);
console.log(numbers);

Categories

Resources