Regex to restrict a string for no more than 5 consecutive numeric characters and no more than 8 numeric characters in total
A negative lookahead with some alternation might be helpfull here:
^(?!.*\d{6}|(?:.*\d){9})[A-Za-z\d]+$
See an online demo
^
- Start-line anchor.(?!
- Open negative lookahead:.*\d{6}
- 0+ Chars other than newline followed by 6 digits.|
- Or:(?:.*\d){9}
- A non-capture group of 0+ chars other than newline followed by a single digit matched 9 times.)
- Close negative lookahead.
[A-Za-z\d]+
- Match 1+ alphanumeric characters.$
- End-line anchor.
How can I match a number that is at least 5-digits-long or more using RegEx?
You can use \d{5,}
, which matches 5 digits or more, then:
- If you want this number as a word use
\b\d{5,}\b
.\b
matches word boundaries. - If that’s a number on its own line, use
^\d{5,}$
.^
matches the beginning of the line while$
matches its end.
Here is an example.
Regular expression: Should not start with 5 digits (or more) in a row
If your RegExp flavor of choice supports negative lookaheads, this pattern will match if the string is valid (does not start with 5 or more consecutive digits):
^(?!\d{5,})
Regex101
Matches:
- 1234testing1234
- testing12345
- testing
Does not match:
- 12345testing123asd
Check if a string contains number with more than 5 digits
MySQL's regular expression engine does not implement the \d
"digit" expression, but instead you can represent it either as a character class range like [0-9]
or as the special character class [[:digit:]]
. The curly brace repeat syntax {5,}
is supported in the form you've attempted.
The available regular expression syntax is described in the manual
So you can use either of the following forms:
title REGEXP '[0-9]{5,}'
title REGEXP '[[:digit:]]{5,}'
Examples:
Non matching:
> SELECT '123' REGEXP '[[:digit:]]{5,}';
+--------------------------------+
| '123' REGEXP '[[:digit:]]{5,}' |
+--------------------------------+
| 0 |
+--------------------------------+
> SELECT '1X345' REGEXP '[0-9]{5,}';
+--------------------------------+
| '123' REGEXP '[0-9]{5,}' |
+--------------------------------+
| 0 |
+--------------------------------+
Matching examples:
> SELECT '98765 Some text here' REGEXP '[[:digit:]]{5,}';
+-------------------------------------------------+
| '98765 Some text here' REGEXP '[[:digit:]]{5,}' |
+-------------------------------------------------+
| 1 |
+-------------------------------------------------+
> SELECT 'Some text here 123456' REGEXP '[0-9]{5,}';
+--------------------------------------------+
| 'Some text here 123456' REGEXP '[0-9]{5,}' |
+--------------------------------------------+
| 1 |
+--------------------------------------------+
1 row in set (0.00 sec)
Regex pattern to match first 5 digit
You can use
/(?<!x)(?<!\border\s*number\W*)(?=(?:[._ –-]*\d){9})(?!9|66\D*6|00\D*0|(?:\d\D*){3}0\D*0|(?:\d\D*){5}0(?:\D*0){3})\d(?:[._ –-]*\d){4}/gi
See the regex demo. Details:
(?<!x)
- a location not immediately preceded withx
orX
(the pattern is case insensitive due toi
flag)(?<!\border\s*number\W*)
- immediately on the left, there cannot beorder
numberwhole word with any amount of whitespaces between the two words and any amount of non-word chars between
number` and the next digit(?=(?:[._ –-]*\d){9})
- immediately on the right, there must be nine occurrences of zero or more dashes, underscore, space or hyphens and a digit(?!9|66\D*6|00\D*0|(?:\d\D*){3}0\D*0|(?:\d\D*){5}0(?:\D*0){3})
- immediately on the right, there can be no9
, or66
+ zero or more non-digits and then6
, or00
+ zero or more non-digits and then0
, or three occurrences of a digit and then any zero or more non-digit chars and then0
, any zero or more non-digit chars and then a0
, or five occurrences of a digit and then any zero or more non-digit chars and then0
, and three occurrences of any zero or more non-digit chars and then a0
\d
- a digit is matched(?:[._ –-]*\d){4}
- and the four sequences of any zero or more spaces, dashes, hyphens or underscores and then a digit.
Regex to match string of limited length and if number digit are present it can't be more than 5 digit
If there can not be more than 5 digits in total, that means you should not be able to match 6 digits.
You can use a negative lookahead to assert what is on the right can not match 6 digits.
^(?!(?:[^\d\r\n]*\d){6})[a-zA-Z0-9]{5,15}$
Explanation
^
Start of string(?!
Negative lookahead, assert what is at the right is not(?:[^\d\r\n]*\d){6}
Match 6 times any char except a newline or a digit, then match a digit
)
Close lookahead[a-zA-Z0-9]{5,15}
Match 5-15 times any of the listed in the character class$
End of string
Regex demo
Note that using [1-9]
in a character class does not match the 0, and \d
will
About the patterns in the question
^(?=.*\d?.*\d?.*\d?.*\d?.*\d?).{0,15}$
Here, the lookahead will always be true as all the parts in it are optional. It could also match an empty string as the quantifier {0,15}
starts at 0, which makes it optional.
^(?=[a-zA-Z1-9]{5,15}$)[a-zA-Z]{1,15}[1-9]{0,5}$
The pattern asserts a string with 5-15 times any of the listed in the character class. But the matching starts with 1-15 times a char a-zA-Z followed by matching 0-5 times a digit at the end of the string.
^(?=.*\d){0,5}.{0,15}$
The pattern optionally asserts 0-5 digits which is always true as it is optional. Then it matches 0-15 times any char.
Related Topics
Extension Mysqli Is Missing, Phpmyadmin Doesn't Work
Dynamic Menu and Role Permission in Codeigniter
How to Show Blob Image in HTML
Regex - Exclude "-" Between Numbers
Running a Python Script from PHP
How to Use a Findby Method With Comparative Criteria
Setting Selected Option in Laravel Form
Fatal Error: Call to Undefined Function Sqlsrv_Connect()
How to Convert Ereg Expressions to Preg in PHP
How to Parameterize the Table Name in a Prepared Statement
Getting a File to Download Instead of Opening the Browser
Php Curl Required Only to Send and Not Wait for Response
A Way to Determine Browser Width in PHP Without JavaScript
Checking Dates If Between Range of Dates in Laravel 5
Convert Doc/Docx Files to Pdf Using PHP
How to Update Multiple Select Box Values in PHP MySQL
Check If a Specific Value Exists At a Specific Key in Any Subarray of a Multidimensional Array