I want to validate number that don't contain the minus char. The number > 0.
Have you got a regex for that ?
Exclusivly non-negativ numbers with decimal-point: ^\d+(?:.\d+)?$, or capturing with negativ look-behind ((?<!-)[[:digit:]]+) or a myriad other ways depending on the flavour of regex you need and the real problem at hand.
To match absolutes
^\d+$
https://regex101.com/r/O4nGl5/2
To match decimals
^\d+(\.?\d+)?$
https://regex101.com/r/O4nGl5/3
There multiple ways to do that, one of them is:
^[0-9]+$ (for integer numbers)
It checks your input against:
Starts and ends with an integer (^ for beginning and $ for end)
characters between between 0 and 9 (integers)
1 or more occurence (+ for 1 or more occurrences of the previous expression)
Related
I need to match a string that can be of length from 1 to 20 characters maximum, and it contains letters a-g and numbers 1-7. However, the numbers cannot be next to each other - only single digit numbers are allowed.
Valid strings: aabbca1a6, 4gg1g2g1, 1
Invalid string: aabbca16a - theres two numbers next to each other, forming a two digit number 16.
I can match most strings quite easily with [a-g1-7]{1,20}, however i have no idea how to detect when two numbers are next to each other efficiently.
Currently in my program, after parsing through the regex, i'm just going through the whole string again in a loop, making sure there's no 2 numbers next to each other, however i'd prefer if it all could be done with just one (simple) regex.
You can use the answer from the comments by Ulugbek Umirov using negative lookahead at the start of an anchored string asserting not 2 digits to the right.
^(?!.*\d{2})[a-g1-7]{1,20}$
The pattern matches:
^ Start of string
(?!.*\d{2}) Negative lookahead, assert not 2 digits
[a-g1-7]{1,20} Repeat the ranges in the character class 1-20 times
$ End of string
Regex demo
Another option could be asserting the string length and repeat matching in a way that there can not be 2 digits next to each other
^(?=[a-g1-7]{1,20}$)[a-g]*(?:[1-7][a-g]+)*[0-7]?$
Regex demo
The simplest regex and approach is to check if it doesn't match:
.*\d\d.*
The best way to solve this problem is to use this trick
-Check if number between 1-7 and character between a-g and the numbers are not siblings with each other by using this pattern
^[1-7]?([a-g]+[1-7]?)*
-Then you can check the length of string using string methods like (length method in JavaScript)
I want to build a regex that will extract me numbers from a string. The pattern is
">number<"
Now the number can have decimals or not.
I went with:
"[^\d]+"
This does extract the numbers but because of decimals, it sometimes works bad. Any ideas?
>((\-|\+)?[0-9]+(\.[0-9]+)?)<
Explained:
(\-|\+)? optional sign: - or +
[0-9]+ - non-empty sequence of digits
(\.[0-9]+)? - optional sequence starting with a dot followed by non-empty sequence of digits
> and < at the beginning and at the and (not in the matching group)
In the first matching group you will have your number.
Example here.
Try this (copy with the quotes):
">[0-9]+(\.[0-9]+)?<"
A simple regex which works for integers, floats and negative numbers :
>([\+\-]?\d+\.?\d*)<
The number is in group 1.
If you can use positive lookarounds, this regex matches just a number between >< and nothing else :
(?<=>)[\+\-]?\d+\.?\d*(?=<)
Here in action.
Assuming that it's just the number you want:
(?<=\\>)[0-9]+(\.[0-9]+)?(?=\<)
It matches any number with or without decimals between > and < but excluding > and <
I'm looking to build a regex that matches the following group of numbers:
10xxxxxxx
1116xxxxx
143xxxxxx
146xxxxxx
149xxxxxx
159xxxxxx
16xxxxxxx
(note the length is always 9)
where x is any digit. My best attempt yielded this:
/^1[01456][1369]*[6]*[0-9]$/
However, I can't get the length of the string to always be 9. Any ideas?
Edit: Maybe I wasn't clear enough, it needs to match those 7 cases, and ONLY those, inclusively and exclusively.
How about:
^1(?:[06]\d{2}|116|4[369]\d|59\d)\d{5}$
use this pattern
^1[01456](16|3\d|6\d|9\d|\d\d)\d{5}$
Is this what you want?
^(?=[0-9]{9}$)(?:10|1116|143|146|149|159|16)
Demo
This starts by looking at the beginning of the string for exactly 9 digits using a positive lookahead anchored to the end of the string. Then we look for any of your 7 specific groups of numbers that the string can start with.
You can use this regex:
/^1[01456][1369][0-9]{6}$/
Since 3 digits are already matched by first 3 patterns 1, [01456] and [1369] so last one must match exact 6 characters to enforce it a 9 digit input.
I'm real newbie when it comes to Regex so apologies if this 'should' be easy.
I need to match the last 6 digits of a number that has the following format
308950 3200 014559
The first 2 groups of numbers will remain constant (308950 3200) and don't need to be extracted. I am only interested in the last 6 digits.
The full number may contain spaces but these need to be optional.
This has to be done in Regex.
Use regex pattern
(?<=\b308950\s*3200\s*)\d{6}\b
or
\b308950\s*3200\s*(\d{6})\b
This should do it even if there are spaces between the digits
^308950 3200[\d\s]*?((\d\s?){6})$
Group 1 will contain the reqired digits with spaces if any
If the leading numbers will remain constant, you can use:
308950 3200\s*(\d{6})
Alternatively, you could use:
(?:\d+\s)+(\d{6})
Also, if the string will be at the end of the input string, consider adding a $ to the end to signify this (to make sure it'll match the end of the string):
(\d{6})$
I have two regular expressions that validate the values entered.
One that allows any length of Alpha-Numeric value:
#"^\s*(?<ALPHA>[A-Z0-9]+)\s*"
And the other only allows numerical values:
#"^\s*(?<NUM>[0-9]{10})"
How can I get a numerical string of the length of 11 not to be catched by the NUM regex.
I think what you're trying to say is that you don't want to allow any more than 10 digits. So, just add a $ at the end to specify the end of the regex.
Example: #"^\s*(?[0-9]{10})$"
Here's my original answer, but I think I read you too exact.
string myRegexString = `#"(?!(^\d{11}$)` ... your regex here ... )";
That reads "while ahead is not, start, 11 digits, end"
If it's single line, you could specify that your match must happen at the end of the line, like this in .net ...
^\s*([0-9]{10})\z
That will accept 1234567890 but reject 12345678901.
Do you mean you want to match up to 10 digits? Try this:
#"^\s*[0-9]{1,10}\s*$"
If you are trying to match only numbers that are 10 digits long, just add a trailing anchor using $, like this:
^\s*(?:[0-9]{10})\s*$
That will match any number that is exactly 10 digits long (with optional space on either side).
var pattern =/\b[0-9]{10}$\b/;
// the b modifier is used for boundary and $ is used for exact length
Match something non-numeric after the length 10 string. My regex-foo isn't that good, but I think you've got it setup there to catch a numeric string of exactly length 10, but since you don't match anything after that, a length 11 string would also match. Try matching beyond the end of the number and you'll be good.
This should match only 10 digits and allow arbitrary numbers of whitespaces before and after the digits.
Non-capturing version: (only matches, the matched digits are not stored)
^\s*(?:\d{10})\s*$
Capturing version: (the matched digits are available in subgroup 1, as $1 or \1)
^\s*(\d{10})\s*$
You could try alternation?
^\s*(?\d{1,10}|\d{12,})