Match any number BUT positive 1-10 - regex

I've tried the following without any success
^((\d{1,})\b[^1-10]\b)$
What should match:
"11", "0", "-1", "100" and any number which is not 1-9
What shouldn't match:
"10", "1", "abc11", "11abc", "a11b" and any number >= 1 AND <= 10

It seems you want
^(?!(?:[1-9]|10)$)-?\d+$
See the regex demo. Details:
^ - start of string
(?!(?:[1-9]|10)$) - a negative lookahead that fails the match if there is a digit from 1 to 9 or 10 substring followed with end of string position immediately to the right of the current location
-? - an optional -
\d+ - one or more digits
$ - end of string.
If you cannot use lookarounds, you can use
^(-[0-9]+|0|1[1-9]|[2-9][0-9]|[0-9]{3,})$
See this regex demo. This regex matches:
^ - start of string
( - start of a capturing group:
-[0-9]+| - - and then one or more digits, or
0| - 0, or
1[1-9]| - 1 and then a non-zero digit, or
[2-9][0-9]| - a digit from 2 to 9 and then any one digit, or
[0-9]{3,} - any three or more digits
) - end of the group
$ - end of string

This solution is not very elegant, but should match the data and rules you posted:
^((-1|0)|(\d{2,}))$
Mini description of the parts of the expression:
^ Start of string
(-1|0) the two "edge case" (Yes, hardcoded)
(\d{2,}) atleast two digits
$ end of string
(,) just grouping, to

Related

Regex - Validation of numeric with up to 10 decimal places (0.01 - 100.0000000000)

I am just curious if this regex expression will be able to be shorten. it should allow ten digits.
/^(?!0+(?:\.0+)?$)(\d{1,2}\.\d{1,10}|\d{1,2}|(100)|(100\\.00)|(100\\.0000000000)|(100\\.000000000)|(100\\.00000000)|(100\\.0000000)|(100\\.000000)|(100\\.00000)|(100\\.0000)|(100\\.0000)|(100\\.000)|(100\\.0))$/
You need to use
/^(?![0.]+$)(?:\d{1,2}(?:\.\d{1,10})?|100(?:\.0{1,10})?)$/
See the regex demo.
Details:
^ - start of string
(?![0.]+$) - no only 0 and/or . chars till end of string are allowed
(?: - either of
\d{1,2}(?:\.\d{1,10})? - one or two digits and then an optional occurrence of a . and one to ten digits
| - or
100(?:\.0{1,10})? - 100 and then an optional occurrence of a . and one to ten 0 chars
) - end of the group
$ - end of string.

regex for matching latitude, longitudes without any character

I am looking for one regex which strictly allows 2 floating point numbers which are comma separated.
Test cases:
0,0
0.021312311323,0
0,0.012312312312
1.1,0.9836373
Regex that I have tried is
^[-+]?([1-8]?\d(\.\d+)?|90(\.0+)?),\s*[-+]?(180(\.0+)?|((1[0-7]\d)|([1-9]?\d))(\.\d+)?)$\D+|\d*\.?\d+
These are latitudes and longitudes but I just want 2 values in these paremeters.
This regex fails in:
-10a, 10a
10a,10b
I would really appreciate any help and guidance.
Your regex ends with a couple of redundant patterns, you should remove \D+|\d*\.?\d+ after $. As $ means the end of string, there can be no more text after it, and the \D+|\d*\.?\d+ requires one or more non-digit chars, or just matches any float or integer number with \d*\.?\d+ - this matched your unwelcome strings.
You can use
^([-+]?(?:[1-8]?\d(?:\.\d+)?|90(?:\.0+)?)),\s*([-+]?(?:180(?:\.0+)?|(?:1[0-7]\d|[1-9]?\d)(?:\.\d+)?))$
See the regex demo. Note I converted some capturing groups into non-capturing, so that there remain just two "notional" capturing groups in the pattern.
Details
^ - start of string
([-+]?(?:[1-8]?\d(?:\.\d+)?|90(?:\.0+)?)) - Group 1:
[-+]? - an optional - or +
(?:[1-8]?\d(?:\.\d+)?|90(?:\.0+)?) - either a number from 0 to 89 ([1-8]?\d) and then an optional fractional part ((?:\.\d+)?) or 90 and then an optional . followed with one or more 0 chars
,\s* - a comma and 0+ whitespace chars
([-+]?(?:180(?:\.0+)?|(?:1[0-7]\d|[1-9]?\d)(?:\.\d+)?)) - Group 2:
[-+]? - an optional - or +
(?:180(?:\.0+)?|(?:1[0-7]\d|[1-9]?\d)(?:\.\d+)?) - either a 180 number followed with an optional . + one or more 0 chars, or a number from 0 to 179 and then an optional fractional part
$ - end of string.
Your regular expression is almost correct. You should have stopped at $ indicating the end of the string.
const testCases = [ "0,0",
"0.021312311323,0",
"0,0.012312312312",
"1.1,0.9836373",
"-10a, 10a",
"10a,10b"];
const re = /^[-+]?([1-8]?\d(\.\d+)?|90(\.0+)?),\s*[-+]?(180(\.0+)?|((1[0-7]\d)|([1-9]?\d))(\.\d+)?)$/g;
testCases.forEach(tc => {
if(tc.match(re)) {
console.log(" VALID : " + tc );
} else {
console.log("NOT VALID : " + tc);
}
});

Regex: to match unsigned integer values (valid values: 0 to 65535 only) for comma separated values

I have
([0-5]?\d?\d?\d?\d|6[0-4]\d\d\d|65[0-4]\d\d|655[0-2]\d|6553[0-5])
which works for single input as:
0
1
65
6553
but i want them for comma separated input string as:
0,1,65,6553 ->this is a valid string
65535,-1,25 ->this is a invalid string because of negative number.
please can anyone suggest solution
Note:
I have already tried repetition as:
^([0-5]?\d?\d?\d?\d|6[0-4]\d\d\d|65[0-4]\d\d|655[0-2]\d|6553[0-5])+(,(([0-5]?\d?\d?\d?\d|6[0-4]\d\d\d|65[0-4]\d\d|655[0-2]\d|6553[0-5])))*$
which is accepting 65537 also which is undesirable.
Checking number bounds afterwards seems more straightforward to me, but anyway, this is a regex you may use (I refactored the integer part a little bit)
^(([0-5]?\d{0,4}|6[0-4]\d{3}|65[0-4]\d{2}|655[0-2]\d|6553[0-5])(,|(?=$)))+$
https://regex101.com/r/1RpNuy/1
Details:
^ : String start
( : Group start
([0-5]?\d{0,4}|6[0-4]\d{3}|65[0-4]\d{2}|655[0-2]\d|6553[0-5]) : Match a number
(,|(?=$)) : Match either , or make sure this is end of line (but without reading the $)
)+ : End of group, repeat as many times as possible
$ : End of string
You may use
^(?:\d{1,4}|[1-5]\d{4}|6[0-4]\d{3}|65[0-4]\d{2}|655[0-2]\d|6553[0-5])(?:,(?:\d{1,4}|[1-5]\d{4}|6[0-4]\d{3}|65[0-4]\d{2}|655[0-2]\d|6553[0-5]))*$
In PCRE and Onigmo, you may use a shorter pattern where \g<1> repeated Group 1 pattern:
^(\d{1,4}|[1-5]\d{4}|6[0-4]\d{3}|65[0-4]\d{2}|655[0-2]\d|6553[0-5])(?:,\g<1>)*$
See the regex demo and regex demo #2
The regex is basically ^<BLOCK>(?:,<BLOCK>)*$ where the BLOCK pattern is a regex matching the numbers from 0 to 65535:
\d{1,4} - 1, 2, 3 or 4 digits (0 - 9999)
[1-5]\d{4} - 1 to 5 digit and then any 4 digits (10000 - 50000)
6[0-4]\d{3} - 6, then a digit from 0 to 4, and then three digits (60000 - 64999)
65[0-4]\d{2} - 65, a digit from 0 to 4 and then any two digits (65000 - 65499)
655[0-2]\d - 655, a digit from 0 to 2 and then any digit (65500 - 655299)
6553[0-5] - 6553 and then a digit from 0 to 5 (65530 - 65535)
The general pattern:
^ - start of string
<BLOCK> - BLOCK pattern described above
(?:,<BLOCK>)* - 0 or more repetitions of , and then BLOCK pattern
$ - end of string.

Regular expression to get positive integer and -1

Below is the text I hope to match:
00000001,00000002,00000003
It works fine with ((([-1-9]+),)+)?[-1-9]+.
But it didn't match -1. The expression must not match with -2 or anything else except -1.
You may use
^(?:0*[1-9][0-9]*|-1)(?:,(?:0*[1-9][0-9]*|-1))*$
See the regex demo.
Pattern details:
^ - start of string
(?:0*[1-9][0-9]*|-1) - a non-capturing group matching...
0*[1-9][0-9]* - zero or mor 0 chars, followed with a non-zero digit followed with any 1 or more digits
| - or
-1 - a -1 substring
(?:,(?:0*[1-9][0-9]*|-1))* - a non-capturing group quantified with * (0 or more) quantifier matching 0 or more repetitions of:
, - a comma
(?:0*[1-9][0-9]*|-1) - same subpattern as in the beginning (-1 or a non-zero number with no fractions)
$ - end of string.
[-1-9]+ doesn't match what you're expecting it to match. It matches for example: "-31-23", which is obviously not a number.
A simple regex like:
(?:^-1)$|^[0-9]+
will match "-1", or any positive integer (including 0001, 00000002, etc...).
Also, depending on the language you're using, it would be simpler to use the language's features to decide if the number is "-1" or any other positive number.
As your state that ((([-1-9]+),)+)?[-1-9]+ works fine which captures a positive integer and looking at the title of the question, you might use this regex using alternation to capture -1 or only positive integers including 0 or 00000 from a string which could be preceded with zeroes.
The positive integers will be captured in group 1.
-[02-9][0-9]*|0*(-?[0-9]+)
Details
- Match literally
[02-9][0-9]* Match a 0 or digits 2-9 followed by zero or more times a digit. Note that the - is not part of the character class or else --- would also match.
| Or
0* Match zero or more times a zero
(-?[0-9]+) Capture in group 1 an optional hyphen followed by one or more times a digit

Matching numbers with an optional delimiter in between

So i got this regex code /[0-3][0-9][0-1][1-9]\d{2}[-\s]\d{4}?[^0-9]*|[0-3][0-9][0-1][1-9]\d{2}\d{4}/
This regex code take this kind of numbers:
1002821187
100282 1187
100282-1187
But i found out i dont want the numbers: 1002821187
So is it possible to make 1 regex code that only finds:
100282 1187
100282-1187
Your regex contains an alternation that matches the numbers with and without the space or -. You need to require that space or hyphen:
^[0-3][0-9][0-1][1-9][0-9]{2}[-\s][0-9]{4}$
^^^^^
See the regex demo. If you do not need to check for any boundaries, remove ^ and $ anchors that make the pattern match the whole string and use [0-3][0-9][0-1][1-9][0-9]{2}[-\s][0-9]{4}. Or use word boundaries to find whole words, \b[0-3][0-9][0-1][1-9][0-9]{2}[-\s][0-9]{4}\b.
Details
^ - start of string
[0-3] - a digit from 0 to 3
[0-9] - any digit
[0-1] (=[01]) - 0 or 1
[1-9] - any digit other than 0
[0-9]{2} - 2 digits
[-\s] - a - or whitespace
[0-9]{4} - 4 digits
$ - end of string.
Though you did not specify exactly what you are trying to do, here you can try the following regex :
\b\d{6}-\d{4}\b|\b\d{4}\b|\b\d{6}\b
Hope it helps.