Regex - how to match everything except a particular pattern

regex match everything except pattern
regex match everything except word
regex match but exclude
regex remove everything except match
regex everything except character
regex exclude pattern from result
regex exclude string
regex negative lookahead

I'm using Apache and I want to redirect all received request to the ssl virtual host.

So I have the following line in the regular http virtual host:

RedirectMatch (.*)$1

which basicaly replace $1 by everything.

It works perfectly. But now, I need to access a particular CGI that cannot be on the SSL virtual host. So I would like to redirect all request, except the following:


I have search on this forum and found some post concerning regex exclusion, but none is working. Any help would be greatly appreciated.

Thanks. Alain

Apache 2.2 and later has negative lookahead support in regular expressions. If you are using Apache 2.2 or later this should work:

RedirectMatch ^/(?!mycgi/cgi.php)(.*)$1

Match everything except for specified strings, If you want to make sure that the string is neither red, green nor blue, caskey's answer is it. What is often wanted, however, is to make sure that� I'm faced with a situation where I have to match an (A and ~B) pattern. The basic regex for this is frighteningly simple: B|(A) You just ignore the overall matches and examine the Group 1 captures, which will contain A. An example (with all the disclaimers about parsing html in regex): A is digits, B is digits within <a tag

I believe the RedirectMatch is a short-circuit sorta deal. What this means, is that if you put another RedirectMatch ahead of your match-all, only the first match will execute. so something like...

RedirectMatch (/mycgi/cgi.php)$1 
RedirectMatch (.*)$1 

Regex: match everything but specific pattern, Not a regexp expert, but I think you could use a negative lookahead from the start , e.g. ^(?!foo).*$ shouldn't match anything starting with foo . Matching any text but those matching a pattern is usually achieved with splitting the string with the regex pattern. Examples : c# - Regex.Split(text, @"red|green|blue") or, to get rid of empty values, Regex.Split(text, @"red|green|blue").Where(x => !string.IsNullOrEmpty(x)) (see demo )

The previous answers are correct, but what if tomorrow there will be another exception? You'll get a fat, hard understand regex. Is better (easier to understand and maintain) to use an If directive expression with Apache internal variables.

<If "%{REQUEST_URI} !~ m#^/mycgi/cgi.php$# && \
     %{REQUEST_URI} !~ m#^/anothercgi/cgi.php$#">
    RedirectPermanent / https://%{HTTP_HOST}/%{REQUEST_URI}

5.4. Find All Except a Specific Word - Regular Expressions , Discussion. 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 . Since we only marked one piece of text for this field that doesn’t fit any particular pattern, RegexMagic selected the “literal text” pattern for this field, making it match the word “bold” that we marked. In the “pattern to match field” drop-down list, select “match anything”.

all except word, Regular Expression to. Top Regular Expressions. Match an MD5 hash � Website Image � identify group � silly html � Match IDs � UnsignedWithoutU Take this regular expression: /^[^abc]/. This will match any single character at the beginning of a string, except a, b, or c. If you add a * after it – /^[^abc]*/ – the regular expression will continue to add each subsequent character to the result, until it meets either an a, or b, or c.

Regex: match everything but specific pattern, *$ shouldn't match anything starting with foo . Regex – How to match everything except a particular pattern?, This example matches three digits other than 999 999� Regular expression to match a line that doesn't contain a word? How do you access the matched groups in a JavaScript regular expression? How do you use a variable in a regular expression? Regex-how to match everything except a particular pattern ; What regex will match every character except comma ',' or semi-colon ';'?

The Best Regex Trick, Describes a regular expressions technique to match a word or pattern except in certain contexts: excluding In contrast, a "trick" is not a single point of syntax such as a negated character class or a lazy quantifier. Match Everything Except X Solution: Use the Java Pattern and Matcher classes, supply a regular expression (regex) to the Pattern class, use the find method of the Matcher class to see if there is a match, then use the group method to extract the actual group of characters from the String that matches your regular expression.

  • Thank you very much. It works perfectly. You resolved in less then 30 minutes what I was working on for almost 8 hours now!
  • +1 You beat me by a few seconds. Your first rule should redirect to 'http' and you might want to add a leading '/' to it as well.
  • Won't that first rule result in a redirect loop?
  • Wow... it was fast!!! But unfortunately by putting this line in my "normal non SSL" virtual host, it does not work. Firefox give me that error: Firefox has detected that the server is redirecting the request for this address in a way that will never complete. And I think this is normal because we ask Apache to do a redirect. So Apache receive the request, match it against the first regex then redirect to himself, match again, and so on. Your idea about that? I tryed to place the line in my SSL virtual server but it is ignored. Alain