如何匹配不包含单词的行 [英] How to match a line not containing a word

查看:44
本文介绍了如何匹配不包含单词的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何使用 Python 样式的正则表达式匹配不包含特定单词的行(只使用正则表达式,不涉及 Python 函数)?

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

示例:

PART ONE OVERVIEW 1 
Chapter 1 Introduction 3

我想匹配不包含单词PART"的行?

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

推荐答案

这应该有效:

/^((?!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)/

编辑(按要求):为什么这种模式有效

(?!...) 语法是一个 否定前瞻,我一直觉得很难解释.基本上,它的意思是这点后面的任何内容都不能与正则表达式 /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.

((?!xxx).)* 习语可能是最难理解的.正如我们所见,(?!PART) 查看前面的字符串并说接下来的任何内容都不能匹配子模式 /PART/.因此,我们对 ((?!xxx).)* 所做的是逐个字母地遍历字符串并将规则应用于所有字符串.每个字符都可以是任何字符,但如果您使用该字符及其后的几个字符,则最好不要使用 PART 一词.

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.

^$ 锚点要求将规则从头到尾应用于整个字符串.如果没有这些锚点,任何不以 PART 开头的字符串都会匹配.甚至 PART 本身也会有匹配项,因为(例如)字母 A 后面没有确切的字符串 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.

因为我们有 ^$,如果 PART 在字符串中的任何位置,其中一个字符将匹配 (?=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.

这篇关于如何匹配不包含单词的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆