Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to match a line not containing a word [duplicate]

Tags:

regex

I was wondering how to match a line not containing a specific word using Python-style Regex (Just use Regex, not involve Python functions)?

Example:

PART ONE OVERVIEW 1  Chapter 1 Introduction 3 

I want to match lines that do not contain the word "PART"?

like image 751
Tim Avatar asked Jun 07 '11 00:06

Tim


People also ask

How do you say does not contain in regex?

In order to match a line that does not contain something, use negative lookahead (described in Recipe 2.16). Notice that in this regular expression, a negative lookahead and a dot are repeated together using a noncapturing group.

How do you use negation in regex?

Similarly, the negation variant of the character class is defined as "[^ ]" (with ^ within the square braces), it matches a single character which is not in the specified or set of possible characters. For example the regular expression [^abc] matches a single character except a or, b or, c.

What is ?! In regex?

Definition and Usage. The ?! n quantifier matches any string that is not followed by a specific string n. Tip: Use the ?= n quantifier to match any string that IS followed by a specific string n.


1 Answers

This should work:

/^((?!PART).)*$/ 

If you only wanted to exclude it from the beginning of the line (I know you don't, but just FYI), you could use this:

/^(?!PART)/ 

Edit (by request): Why this pattern works

The (?!...) syntax is a negative lookahead, which I've always found tough to explain. Basically, it means "whatever follows this point must not match the regular expression /PART/." The site I've linked explains this far better than I can, but I'll try to break this down:

^         #Start matching from the beginning of the string.     (?!PART)  #This position must not be followed by the string "PART". .         #Matches any character except line breaks (it will include those in single-line mode). $         #Match all the way until the end of the string. 

The ((?!xxx).)* idiom is probably hardest to understand. As we saw, (?!PART) looks at the string ahead and says that whatever comes next can't match the subpattern /PART/. So what we're doing with ((?!xxx).)* is going through the string letter by letter and applying the rule to all of them. Each character can be anything, but if you take that character and the next few characters after it, you'd better not get the word PART.

The ^ and $ anchors are there to demand that the rule be applied to the entire string, from beginning to end. Without those anchors, any piece of the string that didn't begin with PART would be a match. Even PART itself would have matches in it, because (for example) the letter A isn't followed by the exact string PART.

Since we do have ^ and $, if PART were anywhere in the string, one of the characters would match (?=PART). and the overall match would fail. Hope that's clear enough to be helpful.

like image 63
Justin Morgan Avatar answered Sep 23 '22 00:09

Justin Morgan