Regex validate number range [duplicate] - regex

This question already has an answer here:
Learning Regular Expressions [closed]
(1 answer)
Closed 6 years ago.
I need to validate string input with regex, rules are:
String should not be number less than 2 and not bigger than 9999 (2-9999)
String should not have zeros before number (ex: no 0002, 0022, 0222)
I really need to accomplish this by regex so any other solution is not acceptable.

Try this:
/^[2-9]|[1-9][0-9]{1,3}$/
To implement your first condition:
String should not be number less than 2 and not bigger than 9999 (2-9999)
There is two cases:
Single digits : [2-9] This is a single character in the range between 2 and 9.
Multiple digits: [1-9][0-9]{1,3} This is a two-three-four-digit number which all digits are in the range 1 and 9.
Note1: {1,3} limits second character class to just accept one or two or three digits.
Note2: ^ means start of string and $ means end of string.
By the way, your second condition isn't defined in pattern above at all. (I mean it doesn't match any number which stars with 0, So all fine.)

Try this
^(?!0|1$)\d{1,4}$
Regex demo
Explanation:
^\d{1,4}$: matches 0-9999
(?!0)...: not have zeros before number (ex: no 0002, 0022, 0222)
(?!1$)...: not be number less than 2 (==1)
(?!…): Negative lookahead sample
\d: One digit from 0 to 9 sample
^: Start of string or start of line depending on multiline mode
$: End of string or end of line depending on multiline mode

Related

trying to understand what this regex means [duplicate]

This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 3 years ago.
Trying to understand what the below regex means.
/^[0-9]{2,3}[- ]{0,1}[0-9]{3}[- ]{0,1}[0-9]{3}$/
Sorry not exactly a coding question.
Let's break this regex into a few different parts:
^: asserts position at start of the string
[0-9]{2,3}: Match a number between 0 and 9, between 2 and 3 times
[- ]{0,1} Matches a dash between zero and one times (Optional dash)
[0-9]{3}: Match a number between 0 and 9, exactly 3 times
[- ]{0,1} Matches a dash between zero and one times (Optional dash)
[0-9]{3}: Match a number between 0 and 9, exactly 3 times
$: asserts position at the end of the string, or before the line terminator right at the end of the string (if any)
Here are a few strings that would pass this regex:
123-123-123
123123123
12-123-123
12123123
Here's a good resource to learn/test regexes: regex101.com
It matches two or three digits followed by (optionally) a dash or space, then 3 digits, again optional dash or space and 3 digits. It seems to try to match a telephone number written in different formats.

Every thousand digit [duplicate]

This question already has answers here:
Only add thousand separator before decimal comma
(3 answers)
Closed 3 years ago.
This is a quiz exercise
Use substitution to put commas in all numbers to separate the thousands. ie: 12345678 → 12,345,678. The number could be in a sentence, and there may be more than one number in the sentence.
I tried the code
/(\d{3})(\d{3})/g
Results returns
Test 6/19: 100013541615681651 should become 100,013,541,615,681,651. Did you forget the global flag?
Tried it
/\d(?=(?:\d{3})+(?!\d))/g
Results returns
Test 14/19: Your regex is incorrectly replacing the number in the following string: Your ticket number is A87654
Regex demo
Matched String
12345678901234567890
12345678
1234567
Unmatched String
A87654
Expected results
123,456789,012345,67890
123,45678
123,4567
A87654
Reference
Matching Whole Lines of Text
Lookahead for a repeated group of 3 digits, followed eventually by negative lookahead for a digit:
\d(?=(?:\d{3})+(?!\d))
and replace with
$0,
https://regex101.com/r/QAHt6E/5
Here's a regex that won't match numbers which are connected to some letters as well:
(?=\b\d|\G)\d+?(?=(?:\d{3})+\b)
(?=\b\d|\G) is a lookahead for either a word break and a digit, or the end of the previous match . \d+? looks for a minimal number of digits and then (?=(?:\d{3})+\b) asserts that it is followed at least one group of 3 digits and then another word break. The word breaks at the beginning and end of the regex mean that it cannot match numbers that are part of an alphanumeric string.
This should be replaced with $0, to insert commas into the numbers.
Demo on regex101

Regex check for specific phone phone numbers or extensions

String to be evaluated will be either be a 10 digit number or a 4 digit number.
5551119900 (10 Digit)
9999 (4 Digit)
Need regex to test for specific list of 10 digit numbers or 4 digit numbers. I have the following Regex that almost works
55511199(00|01|02|10|20|30)|(0000|9901|9902|9903|9999)
Above is checking for
5551119900
5551119901
5551119902
5551119910
5551119920
5551119930
0000
9901
9902
9903
9999
ISSUE:
(1) Need match to be exactly 10 digits or 4 digits only.
(2) Pattern match (see link below) is showing an exact match and also a "Group 1". I'm not sure what the group match means or if that is a good thing.
Sample: https://regex101.com/r/BbplFG/1/
Try this version of your regex:
^(?:55511199(?:00|01|02|10|20|30)|(?:0000|9901|9902|9903|9999))$
Demo
I have made several changes here:
Used ?: inside terms in parentheses, to turn off group capturing
Placed the entire pattern inside parentheses
Added starting (^) and ending ($) anchors around the entire pattern

Regex quantifier not restricting match [duplicate]

This question already has an answer here:
Restricting character length in a regular expression
(1 answer)
Closed 4 years ago.
I would like to match 1 or more capital letters, [A-Z]+ followed by 0 or more numbers, [0-9]* but the entire string needs to be less than or equal to 8 characters in total.
No matter what regex I come up with the total length seems to be ignored. Here is what I've tried.
^[A-Z]+[0-9]*{1,8}$ //Range ignored, will not work on regex101.com but will on rubular.com/
^([A-Z]+[0-9]*){1,8}$ //Range ignored
^(([A-Z]+[0-9]*){1,8})$ //Range ignored
Is this not possible in regex? Do I just need to do the range check in the language I'm writing in? That's fine but I thought it would be cleaner to keep in all in regex syntax. Thanks
The behaviour is expected. When you write the following pattern:
^([A-Z]+[0-9]*){1,8}$
The {1,8} quantifier is telling the regex to repeat the previous pattern, therefore the capturing group in this case, between one to eight times. Due to the greedyness of your operators, you will match and capture indefinitely.
You need to use a lookahead to obtain the desired behaviour:
^(?=.{1,8}$)[A-Z]+[0-9]*$
^ Assert beginning of string.
(?=.{1,8}$) Ensure that the string that follows is between one and eight characters in length.
[A-Z]+[0-9]*$ Match any upper case letters, one or more, and any digits, zero or more.
$ Asserts position end of string.
See working demo here.
The regex ^([A-Z]+[0-9]*){1,8}$ would match [A-Z]+[0-9]* 1 - 8 times. That would match for example a repetition of 8 times A1A1A1A1A1A1A1A1 but not a repetition of 9 times A1A1A1A1A1A1A1A1A1
You might use a positive lookahead (?=[A-Z0-9]{1,8}$) to assert the length of the string:
^(?=[A-Z0-9]{1,8}$)[A-Z]+[0-9]*$
That would match
^ From the start of the string
(?=[A-Z0-9]{1,8}$) Positive lookahead to assert that what follows matches any of the characters in the character class [A-Z0-9] 1 - 8 times and assert the end of the string.
[A-Z]+[0-9]*$ Match one or more times an uppercase character followed by zero or more times a digit and assert the end of the string. $

RegEx for checking if number is less or greater than [duplicate]

This question already has answers here:
Regex for number check below a value
(6 answers)
Closed 8 years ago.
I need expression to check if number 7 is less than 30 and greater than 1. This is an example. Can anybody provide expression and an explanation?
^([2-9]|[1-2][0-9])$
The expression above will match, if:
the given string is one character long and that character is a number ranging from 2 to 9
the given string is two characters long, first character is 1 or 2 and the second character ranges from 0 to 9
Don't use regex, but if you want to, here you go:
^(?:[2-9]|[1-2][0-9])$
Debuggex Demo
Explanation:
This anchors to the beginning/end of the string (so we don't match 7 in the number 175) and then all of the logic happens in the non-capturing group. Either match the numbers [2-9 ] (greater than 1) OR match [1-2] followed by any digit [0-9] (range from 10-29). Notice that I used [0-9] instead of \d because it fits better for readability and \d technically will match other numeric characters (Arabic, etc).
Side note, if you want to allow leading 0's (1 < 007 == 7 < 30), you can allow for 0+ 0's after the start of the string:
^0*(?:[2-9]|[1-2][0-9])$