Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex for all strings not containing a string? [duplicate]

Tags:

regex

Ok, so this is something completely stupid but this is something I simply never learned to do and its a hassle.

How do I specify a string that does not contain a sequence of other characters. For example I want to match all lines that do NOT end in '.config'

I would think that I could just do

.*[^(\.config)]$ 

but this doesn't work (why not?)

I know I can do

.*[^\.][^c][^o][^n][^f][^i][^g]$ 

but please please please tell me that there is a better way

like image 509
George Mauer Avatar asked Dec 28 '09 21:12

George Mauer


People also ask

What does \+ mean in regex?

Example: The regex "aa\n" tries to match two consecutive "a"s at the end of a line, inclusive the newline character itself. Example: "a\+" matches "a+" and not a series of one or "a"s. ^ the caret is the anchor for the start of the string, or the negation symbol.

What does regex 0 * 1 * 0 * 1 * Mean?

Basically (0+1)* mathes any sequence of ones and zeroes. So, in your example (0+1)*1(0+1)* should match any sequence that has 1. It would not match 000 , but it would match 010 , 1 , 111 etc. (0+1) means 0 OR 1.

What is Alnum in regex?

The Alphanumericals are a combination of alphabetical [a-zA-Z] and numerical [0-9] characters, 62 characters.


1 Answers

You can use negative lookbehind, e.g.:

.*(?<!\.config)$ 

This matches all strings except those that end with ".config"

like image 77
Manu Avatar answered Oct 06 '22 07:10

Manu