You have to use a negative lookahead assertion.

(?!^ABC$)

You could for example use the following.

(?!^ABC)

If this does not work in your editor, try this. It is tested to work in ruby and javascript:

^((?!ABC).)*$
Answer from Daniel Brückner on Stack Overflow
🌐
Reddit
reddit.com › r/regex › how to exclude a string while matching another?
r/regex on Reddit: How to exclude a string while matching another?
October 9, 2023 -

Im sorry but I have basically no knowledge of regex so this may be something easy to answer but I can NOT figure it out.

Im using a program called GINA that reads log files of a game. I'm trying to figure out how to trigger based on this string of text

{S} tells you, '{S1}

(Soandso tells you, "Hello")

But I need it to exclude any string containing the word "Master."

(Yourpet tells you, "Attacking creature Master.")

Discussions

Help with regex, exclude strings
Hello, i need some help to define regex I want to whitelist all EU-Amazon AWS: .eu-.+.amazonaws.com$ But I want to exclude some strings: j4fbanners teststring2 teststring3 I tested this one, but exclusions do not work: ((?!^j4fbanners$)|(?!^teststring2$)|(?!^teststring3$)).*.eu-.+.amazonaws.com$ ... More on discourse.pi-hole.net
🌐 discourse.pi-hole.net
6
0
April 13, 2024
sed - How can I exclude a string using a regular expression? - Unix & Linux Stack Exchange
1 Extended Regular Expressions (EREs): How do I include pattern but exclude specific superset of pattern in matches? 4 Using sed to replace a string where the replacement contains whitespace · 11 Why do I have to quote an escaped character in a regular expression for grep, but not on online regex ... More on unix.stackexchange.com
🌐 unix.stackexchange.com
February 10, 2015
REGEX for Strings that have a Certain String, but Exclude those that have a Certain string Prior
I have a query on a Regex expression. Given: I have these strings randomly: 1. Shipment Deadline: 2. Closing Deadline: 3. Cut-Off Deadline: I would need to get any value that would have “Deadline:” But I don’t want the “Shipment Deadline:” or even possibly "Shipment xxxxx Deadline:" How can exclude ... More on forum.uipath.com
🌐 forum.uipath.com
2
0
November 14, 2024
Regex multiple include/exclude string from single regex
Hi all, i need help , I would like to know if what I am trying to do is possible, I am using php to run an API to retrieve company data for a CRM, this API accept regular expressions to do the search, for exemple if i'm literally using “CEO”, i will… More on learn.microsoft.com
🌐 learn.microsoft.com
2
0
🌐
Ytria
docs.ytria.com › globalfeatures › examples-excluding-a-string
Examples - excluding a string | Ytria Documentation
Having used the expression ^((?!.*server.*).)*$ to filter the column, all lines that contained the string server have now been excluded.
🌐
Pi-hole Userspace
discourse.pi-hole.net › help
Help with regex, exclude strings - Help - Pi-hole Userspace
April 13, 2024 - Hello, i need some help to define regex I want to whitelist all EU-Amazon AWS: .eu-.+.amazonaws.com$ But I want to exclude some strings: j4fbanners teststring2 teststring3 I tested this one, but exclusions do not work: ((?!^j4fbanners$)|(?!^teststring2$)|(?!^teststring3$)).*.eu-.+.amazonaws.com$ It should not matter where the string appears or if there are letters before or after the string.
🌐
Coding Horror
blog.codinghorror.com › excluding-matches-with-regular-expressions
Excluding Matches With Regular Expressions
May 7, 2025 - Stating a regex in terms of what you don’t want to match is a bit harder. One easy way to exclude text from a match is negative lookbehind:
Find elsewhere
🌐
Coderanch
coderanch.com › t › 377317 › java › regex-pattern-exclude-substrings-matches
regex pattern to exclude certain substrings from matches (Java in General forum at Coderanch)
July 24, 2005 - That won't work with your second example, since the delimiter is a word character, but the principle is the same: make sure the lookahead doesn't look past the next delimiter, and that the matched text is preceded and followed by a delimiter. Here's a more general approach that will work for your second example: This regex should work for any data with a single-character delimiter--just insert the real delimiter in place of each 'X' (and the string you want to exclude in place of "BAD").
🌐
QBasic on Your Computer
chortle.ccsu.edu › finiteautomata › Section07 › sect07_12.html
Basic Regular Expressions: Exclusions
except a list of excluded characters, put the excluded charaters between [^ and ]. The caret ^ must immediately follow the [ or else it stands for just itself.
Top answer
1 of 2
2
sed '/timetosa/d' <test

...will do it. Alternatively:

sed -n '/timetosa/!p' <test

Still, though (whether it's allowed or not):

grep -v timetosa <test

...is going to be the most performant solution of the three - and probably by a significant margin.

Thanks to @Sparhawk, I found my way to the zaproxy documentation. Based on this:

  • URL regexs
    • In the Include in *, Exclude from * panels and the Logged in/out indicators of the Authentication panel, you can enter regular expressions to define excluded URLs.

...and the following, I would guess you're trying to filter Contexts? According to the docs, you can do both include and exclude lists:

  • Exclude from context
    • This allows you to manage the URLs which will be excluded from the context.
    • You only need to specify regexs for URLs that you do not want to include but which match one or more of the include regexes.

So you can exclude some of your previous inclusions.

Still, though, it may be the first bit of this is not entirely irrelevant - the docs also mention this in the Add-ons section:

  • Invoke Applications

    • Other applications can be invoked passing in context information, such as the URL of the message selected.
    • So, for example, nmap could be invoked passing the site which you want it to scan.

    • Applications are configured using the Options Applications screen.

2 of 2
1

Regular languages are closed under complementation, so for every regular expression, there exists a regular expression that matches exactly the inputs that the original regexp doesn't match.

However, in the worst case, the smallest regexp that matches the complement language has a length that is exponential in the length of the original regexp. So while the regexp is guaranteed to exist, it is not guaranteed to be simple. It can be calculated algorithmically if you really need it.

The ^ operator to anchor a regexp isn't relevant. You may be thinking of ^ in a character set, as in [^a-z] meaning “any one character that isn't a lowercase letter”. This is just part of the shortcut notation for character sets, it isn't helpful to complement a set of strings.

Some regular expression engines, such as perl's or the compatible nd widely-used PCRE, support additional operators beyond the traditional ones including lookaround assertions. A negative lookahead assertion provides a simple way to negate a regular expression without having to break it down into parts. Check the documentation of your software to see what kind of regular expressions it supports.

^(?!.*timetosa)

Or in vim:

^(.*timetosa)\@!

Most systems don't need regexp complementation because you can achieve the same effect by using a matching inversion flag (e.g. grep -v) or by ordering rules carefully in a first-match setting (if it matches .*timetosa.* then do nothing and stop matching rules; if it matches .* then do something).

🌐
RegexOne
regexone.com › lesson › excluding_characters
RegexOne - Learn Regular Expressions - Lesson 4: Excluding specific characters
To represent this, we use a similar expression that excludes specific characters using the square brackets and the ^ (hat). For example, the pattern [^abc] will match any single character except for the letters a, b, or c. With the strings below, try writing a pattern that matches only the ...
🌐
UiPath Community
forum.uipath.com › help › activities
REGEX for Strings that have a Certain String, but Exclude those that have a Certain string Prior - Activities - UiPath Community Forum
November 14, 2024 - Hi Guys! I have a query on a Regex expression. Given: I have these strings randomly: 1. Shipment Deadline: 2. Closing Deadline: 3. Cut-Off Deadline: I would need to get any value that would have “Deadline:” But I don’t want the “Shipment Deadline:” or even possibly "Shipment xxxxx Deadline:" How can exclude matches that would have “Shipment?” System.Text.RegularExpressions.Regex.Matches(in_Str, “(?i)(deadline)(\D|\d){1,40}\d{4}\D\d{2}\D\d{2}”) Thank you!
🌐
regex101
regex101.com › library › iB3fQ4
Regex Include & Exclude
Please complete this security check to continue to regex101.
🌐
Broadcom
knowledge.broadcom.com › external › article › 34711 › regex-how-to-include-and-exclude-in-same.html
Regex How to Include and Exclude in same expression
June 3, 2026 - Examples to include: abchyd zyabc2 zyabc Examples to exclude: zyabcnimbu zynimabcbu If you want to get the ones which have "abc" but not "nim" in them, use the following regex: /^(?!.*nim).*abc.*/
🌐
GitHub
gist.github.com › mence › bc989877b29d588ab99c
How to exclude exact strings using a regular expression · GitHub
How to exclude exact strings using a regular expression · Raw · gistfile1.md · Does not match foo · ^((?!foo).)*$ Result: foo bar <- match baz <- match qux <- match foobar barbaz <- match · Matches exactly foo, bar · ((^|, )(foo|bar))+$ Result: foo <- match bar <- match baz qux foobar barbaz · ^((?!((^|, )(foo|bar))+$).)*$ foo bar baz <- match qux <- match foobar <- match barbaz <- match · regex101 ·
🌐
Adobe Support Community
community.adobe.com › home › app communities › flex (read-only) › questions › regexp for excluding special characters in a string.
RegExp for excluding special characters in a string. | Community
April 21, 2012 - I have tried this expression :----- /^[^///\/</>/?/*&]+$/...But in this it is also negating the alphabets.Also I have tried with opposite condition that in the String we should have alphabets and the expression is:-- ([a-z]|[A-Z]|[0-9]|[ ]|[-]|[_])*..... Please can anyone help me on this. Thanks in advanced to all. ... This topic is closed to new replies. Start a new post to keep the conversation going. ... Thanks for your reply.I have tried this condition. ... Re: RegExp for excluding special characters in a string.
🌐
Wikipedia
en.wikipedia.org › wiki › Regular_expression
Regular expression - Wikipedia
4 days ago - A regular expression (shortened as regex or regexp), sometimes referred to as a rational expression, is a sequence of characters that specifies a match pattern in text. Usually such patterns are used by string-searching algorithms for "find" or "find and replace" operations on strings, or for ...
🌐
O'Reilly
oreilly.com › library › view › regular-expressions-cookbook › 9781449327453 › ch05s04.html
5.4. Find All Except a Specific Word - Regular Expressions Cookbook, 2nd Edition [Book]
August 27, 2012 - Although a negated character class (written as ‹[^⋯]›) makes it easy to match anything except a specific character, you can’t just write ‹[^cat]› to match anything except the word cat. ‹[^cat]› is a valid regex, but it matches any character except c, a, or t.
Authors: Jan GoyvaertsSteven Levithan
Published: 2012
Pages: 609