Exclude certain numbers from range of numbers using Regular expression
Described in more detail in this answer, you can use the following regex with the “Negative Lookahead” command ?!
:
^((?!501|504)[0-9]*)$
You can see the regex being executed & explained here: https://regex101.com/r/mL0eG4/1
/^((?!501|504)[0-9]*)$/mg
- ^ assert position at start of a line
- 1st Capturing group ((?!501|504)[0-9]*)
- (?!501|504) Negative Lookahead - Assert that it is impossible to match the regex below
- 1st Alternative: 501
- 501 matches the characters 501 literally
- 2nd Alternative: 504
- 504 matches the characters 504 literally
- [0-9]* match a single character present in the list below
- Quantifier: * Between zero and unlimited times, as many times as possible, giving back as needed [greedy]
- 0-9 a single character in the range between 0 and 9
- $ assert position at end of a line
- m modifier: multi-line. Causes ^ and $ to match the begin/end of each line (not only begin/end of string)
- g modifier: global. All matches (don't return on first match)
RegEx: number range excluding specific number
You may use a negative lookahead assertion in your regex:
~\bAM19/0(?!803)[678]\d{2}\b~
RegEx Demo
Here we have a negative lookahead (?!803)
after matching 19/0
which will fail the match if 803
appears right after 19/0
in input.
Also note that by using an alternate regex delimiter ~
you can avoid escaping /
in your regex.
Regex to exclude a range of numbers
Thank you for the answers (which all work correctly).
Bohemian was also correct in saying that:
it ends with
\.*
but should end with\..*
or more precisely\.\d*
So I edited my regular expression to the following which now works fine:
([0-9][0-9]\.\d*)|(1[0-9][0-9]\.\d*)
0-100 number range regex excluding 1 and 3
Detect any given number in the range of 0 to 100 , however excluding 1 and 3
^(?!(?:1|3)$)(?:[0-9]{1,2}|100)$
See demo
In case the numbers are not standalone strings:
\b(?!(?:1|3)\b)(?:[0-9]{1,2}|100)\b
See another demo
The main point here is a look-ahead in the beginning, anchors (\b
word boundary, ^
start and $
end of string) and the character classes with limiting quantifiers.
The (?!(?:1|3)$)
lookahead makes sure the match fails if a 1
or (|
) 3
appears right after the start of string (^
) and before the end of string ($
) (or between word boundaries as in the second example).
The [0-9]{1,2}
character class matches 1 or 2 digits (due to a limiting quantifier {1,2}
) from 0
to 9
.
How to match range of numbers excluding first digit
This regex enforces the uniqueness of X
, Y
and Z
:
([1-9])((?!\1)[1-9])\10((?!\1|\2)[1-9])\3\2
...but there's no way to enforce their ordering with a regex.
About the regex:
([1-9])
captures the first digit in group #1. That's the first X
in your template.
((?!\1)[1-9])
captures the second digit in group #2, but only after the negative lookahead confirms that it isn't the same as the first digit. That's the Z
value.
\1
matches the third digit, assuming it's the same as the first digit.
0
is obvious
((?!\1|\2)[1-9])
represents the Y
value, so we have to confirm that it's not the same as either of the other two captures. It's captured in group #3.
\3
matches the same digit again; that's the second Y
.
\2
matches another of whatever the Z
value was, and Bob's your uncle!
Getting back to that 0
again, there's one caveat that I overlooked. If there happen to be ten or more capturing groups in the regex, \10
could be interpreted as a backreference to group #10. It's a good idea to break up that kind of thing whether it needs it or not.
Many regex flavors provide alternative notation that isolates the group reference, like \g<1>
or ${1}
. Not knowing what flavor you're using, I'll use square brackets to isolate the zero instead (i.e., turn it into a single-element character class):
([1-9])((?!\1)[1-9])\1[0]((?!\1|\2)[1-9])\3\2
Excluding some character from a range - javascript regular expression
To exclude k
or p
from [a-zA-Z]
you need to use a negative lookahead assertion.
(?![kpKP])[a-zA-Z]+
Use anchors if necessary.
^(?:(?![kpKP])[a-zA-Z])+$
It checks for not of k
or p
before matching each character.
OR
^(?!.*[kpKP])[a-zA-Z]+$
It just excludes the lines which contains k
or p
and matches only those lines which contains only alphabets other than k
or p
.
DEMO
Exclude a certain number from this regex
A negative look-ahead assertion can do this for you
/^(?!2$)([a-zA-Z,\d]){1,5}$/
Regular expression to exclude number from list
If negative lookaheads be allowed, then you can try the following regex:
^(?!(?:1|4|10)$)\d+$
Regex101
Related Topics
How to Get Multiple Value from Post Variable Using Same Name
Sum Value in Foreach Loop Every 3 Time Looping
Removing Specific Elements from Json Encoded Array by Use of PHP
Download a Facebook Video (.Mp4) Via PHP
How to Pass an Array Within a Query String
Php Regex Find Text Between Custom Added HTML Tags
How to Redirect to the Same Page in PHP
How to Pass JavaScript Variables to PHP
Implode a Column of Values from a Two Dimensional Array
Laravel, Sync() - How to Sync an Array and Also Pass Additional Pivot Fields
Get Image Type from Base64 Encoded Src String
General Error: 1364 Field 'User_Id' Doesn't Have a Default Value
Remove Country Code from Phone Number
Running a Python Script from PHP
How to Not Make Phpmailer Send an Email With Multiple 'To' Addresses