This question already has answers here:
Regex for matching something if it is not preceded by something else
(3 answers)
Closed 3 years ago.
I want to match strings containing MYSTUFF in all cases, except when MYSTUFF is preceded by ENABLED_. So that the following would match:
MYSTUFF
m_MYSTUFF
MYSTUFFIsGreat
allOf_MYSTUFF
but the following wouldn't:
ENABLED_MYSTUFF
m_ENABLED_MYSTUFF
ENABLED_MYSTUFFIsGreat
allOf_ENABLED_MYSTUFF
I tried several variations using negative lookahead (variations of \w*(?!.*ENABLED_)MYSTUFF\w*), and conditional (variations of (?(?!=ENABLED_)(MYSTUFF))), but I did not manage to get the results I'm after.
Is what I want even doable with regexes?
You could accomplish this by using a negative look-behind assertion ...
\w*(?<!ENABLED_)MYSTUFF\w*
see regex demo
Related
This question already has answers here:
RegEx to get section of URL after string in Javascript
(2 answers)
Closed 1 year ago.
I’m trying to extract a string between 2 slashes in a url for example:
HTTPS://www.google.com/blabla/ab /what2/what3/lalaalala
I want to extract the ab which is between 2 slashes in the middle. How can I extract it? It always comes after blabla if it helps.
I tried:
([^\/]+$)
You want to use a positive lookbehind for that: (?<=...)
It checks if something exists before your match without capturing it in the result.
Here is the regex:
(?<=\/blabla\/)([^\/]+)
Now you only get ab as a result.
https://regex101.com/r/4gEsfP/1
Note: It's "positive" because it checks whether it is present, and "lookbehind" because it looks behind the ([^\/]+) pattern. There are also negative lookbehinds and positive/negative lookaheads.
Some good resource about it: https://javascript.info/regexp-lookahead-lookbehind
This question already has answers here:
Regular expression to match a line that doesn't contain a word
(34 answers)
Closed 2 years ago.
I am currently using the following character class:
[^\)\(] in my regex
I want to add the word 'hello' to this class so it is also not matched in my string.
I have tried
[^\)\((hello)]
but it does not work.
What can I do?
One typical way you would enforce that hello does not appear would be to use a negative lookahead, e.g.
^(?!.*hello)[^t()]+$
If you only wanted to exclude hello when it appears as a bona fide word, then surround it with word boundaries in the lookahead:
^(?!.*\bhello\b)[^t()]+$
This question already has answers here:
Regular expression to match a line that doesn't contain a word
(34 answers)
How to negate specific word in regex? [duplicate]
(12 answers)
Closed 3 years ago.
I want to validate a string with alpha numeric values, but if the string contains -- (double dash) anywhere in the string, it should be invalid.
valid:
apple123
-apple-123
app-le123
a-p-p-l-e-1-2-3
invalid:
--apple123
app--le123
https://stackoverflow.com/a/1240365/1920590
The above old post have the answer ^(?!.*bar).*$ which does the negation, but it does not work for same character repetition like --.
Can anyone help me to figure out to modify the ^(?!.*bar).*$ to identify -- as a string.
You may use a negative lookahead:
^(?!.*--)[\w-]+$
(?!.*--) is a negative lookahead assertion that will fail the match if -- appears anywhere in input.
[\w-] matches a word character [a-zA-Z0-9_] or a hyphen
RegEx Demo
This question already has answers here:
What does ?! mean?
(3 answers)
Closed 6 years ago.
$str = "doctor_who:eeh1234LMNOP51234.123";
I want to match doctor_who:ee not doctor_who:eeh;
$str =~ m/doctor_who:e(?!eh)[epx];
I want to know the role of regular expression within Parentheses;
A component like (?! ... ) will fail to match if the following characters in the target string match the enclosed regex pattern. It's called a negative look-ahead
It's unclear whether you need help to form a pattern to your requirements, or if you've come across a pattern that you don't understand
If I was writing it, I'd look at your specification
I want to match 'doctor_who:ee' not 'doctor_who:eeh'
You want to match doctor_who:ee that isn't followed by h, which is
/doctor_who:ee(?!h)/
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Regular expression to match string not containing a word?
How can I invert a regular expression in JavaScript?
Say I have the regex foo123. How do I match everything that is not foo123?
Use negative lookahead for this.
(?!foo123).+
matches any string except foo123
If you want to match empty string also, use (?!foo123).*
In your case (according to the comment) the required regex is (?!P[0-9]{1,}).+.
It matches P and 123, but not P123.