Regex for all rows not containing a string?

Okay, so this is something completely stupid, but this is something that I just never learned to do and its trouble.

How to specify a string that does not contain a sequence of other characters. For example, I want to combine all lines that DO NOT end with .config

I would think I could just do

.*[^(\.config)]$ 

but it does not work (why not?)

I know what I can do

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

but please please tell me there is a better way

+19
regex
Dec 28 '09 at 21:47
source share
7 answers

You can use negative lookbehind , for example:

 .*(?<!\.config)$ 

This matches all lines except those ending in ".config"

+39
Dec 28 '09 at 21:52
source share

There are two questions in your question, so here are a few answers.

Match strings that don't contain a specific string (say .config ):

 ^(?:(?!\.config).)*$\r?\n? 

Match lines that do not end on a specific line:

 ^.*(?<!\.config)$\r?\n? 

and, as a bonus: match lines that do not start with a specific line:

 ^(?!\.config).*$\r?\n? 

(every time, including newlines, if any.

Oh, and answer why your version doesn't work: [^abc] means "any (1) character except a, b or c". Your other solution will also fail on test.hg (because it also ends with the letter g - your regular expression looks at each character separately and not at the whole .config line. So you need lookaround to handle this.

+15
Dec 28 '09 at 10:00
source share
 (?<!\.config)$ 

:)

+4
Dec 28 '09 at 21:51
source share

If you are not "grepping" ... since you are not using the result of a match, why not look for lines that end in .config and skip them? In Python:

 import re isConfig = re.compile('\.config$') # List lst is given filteredList = [f.strip() for f in lst if not isConfig.match(f.strip())] 

I suspect this will work faster than the more complex re.

+2
Dec 28 '09 at 21:57
source share

Using the [^] construct, you created a negative character class that matches all characters except the ones you named. The order of characters in the matching candidates does not matter, so this will not work on any line that has any of [(\.config) (or [)gi.\onc(] )

Use a negative lookahead (with perl regular expressions) as follows: (?!\.config$) . This will match all lines that do not match the literal ".config"

+2
Dec 28 '09 at 21:58
source share

As you asked for a "better way": I would try a "filter". I think that reading and understanding is very easy:

 #!/usr/bin/perl while(<>) { next if /\.config$/; # ignore the line if it ends with ".config" print; } 

As you can see, I used the perl code example. But I think you understand this idea?

added : this approach could also be used to combine more filter patterns, and it still remains well-read and understandable,

  next if /\.config$/; # ignore the line if it ends with ".config" next if /\.ini$/; # ignore the line if it ends with ".ini" next if /\.reg$/; # ignore the line if it ends with ".reg" # now we have filtered out all the lines we want to skip ... process only the lines we want to use ... 
+2
Dec 28 '09 at 10:20
source share

I used Regexpal before finding this page, and came up with the following solution when I wanted to check that the line does not contain the file extension:

^(.(?!\.[a-zA-Z0-9]{3,}))*$ I used the m flag so that I could represent a lot of lines and see which of them ran or did not match.

to find a string that does not contain another expression "^(.(?!" + that you do not want + "))*$"

My article on using this particular regular expression

0
Oct 20 '11 at 2:05 a.m.
source share



All Articles