How do you customize strings

Customize everything except the specified strings

I know the following regular expression matches "red", "green", or "blue".

Is there an easy way to do it all with exception to match several specified strings?


If you want to make sure the string isn't red, green, or blue, caskey's answer is. However, what is often desired is to make sure that the line does not contain red, green, or blue anywhere. To do this, anchor the regular expression with and insert it into the negative lookahead:

For example, suppose you want lines that contain the word "engine" but without any of these colors:

You might think you can put that on the regular expression head:

but you can't. You need to have both instances for it to work.

Depends on the language, but there are generally negative claims that you can include like this:

(Thanks for the syntax correction, the above is valid Java and Perl, YMMV)

If you want to match the entire string, matching everything but certain strings, here's how you can do it:

That is, start the match at the beginning of the string where it can't start and end with red, green, or blue, and match everything else to the end of the string.

You can try here:

Note that this only works with regex engines that support negative lookahead.

You don't need a negative lookahead. There is a working example:


  • - match any character
  • - Match from 0 to unlimited from the previous group
  • - fit as little as possible
  • - match one of these words or nothing
  • - Repeat the pattern


Will be:

Try it:

I had the same question, the suggested solutions almost worked, but they had some problems. At the end is the regular expression I used:

I've tested it in Javascript and .NET.

. * should not be inserted into the negative lookahead like this: ^ (?!. * red | green | blue), otherwise the first element would behave differently than the others (ie "anotherred" would not match, while "anothergreen" would )

Adjusting any text that conforms to a pattern is usually done by share of the string with the regex pattern is reached .

Examples :

  • c # - or to get rid of empty values ​​(see demo)
  • or to remove empty elements (see demo or this demo where LINQ is supported)
  • Javascript- (no modifier has to be used here!) (To remove empty values, use), see demo
  • Java, or - to keep all subsequent empty elements - use or to remove all empty elements use more code to remove them (see demo).
  • groovy- Similar to Java to use all subsequent elements and remove all empty elements (see demo).
  • See demo to remove kotlin or blank items
  • Scala- or to keep all subsequent empty elements, use and remove all empty elements, use (see demo)
  • Ruby- to get rid of empty values ​​use (and to get both leading and trailing empty elements use as second argument) (see demo)
  • perl or with all subsequent empty elements or without empty elements (see demo)
  • php- or not to output empty elements (see demo)
  • Python or to remove empty objects (see demo)
  • go- use this code, and if you need to remove blank items use this code. See go demo.

NOTE : If patterns contain capturing groups, regex split functions / methods may behave differently, also depending on additional options. In this case, please read the corresponding documentation on the split method.

All but the word "red"

All but the word "red"

We use cookies and other tracking technologies to improve your browsing experience on our website, to show you personalized content and targeted ads, to analyze our website traffic, and to understand where our visitors are coming from.

By continuing, you consent to our use of cookies and other tracking technologies and affirm you're at least 16 years old or have consent from a parent or guardian.

You can read details in our Cookie policy and Privacy policy.