sed:我的模式可以包含“不是”吗?特点?我怎么说“不是X”?
如何在 sed
中说“不是”某个字符?
How do I say "is not" a certain character in sed
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
如何在 sed
中说“不是”某个字符?
How do I say "is not" a certain character in sed
?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(5)
这是一个接受除
x
之外的任何字符的字符类。This is a character class that accepts any character except
x
.对于那些对所选答案不满意的人,按照约翰尼的评论。
su[^x]
将匹配sum
和sun
,但不匹配su
。您可以使用以下语法告诉 sed
not
与x
匹配行:查看 kkeller 的答案再举一个例子。
For those not satisfied with the selected answer as per johnny's comment.
su[^x]
will matchsum
andsun
but notsu
.You can tell sed to
not
match lines withx
using the syntax below:See kkeller's answer for another example.
您的问题有两种可能的解释。就像其他人已经指出的那样,
[^x]
匹配不是x
的单个字符。但空字符串也不是x
,因此您可能正在寻找[^x]\|^$
。这些答案都不能扩展到多字符序列,而这通常是人们正在寻找的。您可以煞费苦心地构建一些类似于
编写与
str
不匹配的正则表达式的东西,但sed
中更直接的解决方案是删除匹配的任何行 匹配str
,然后保留其余部分;Perl 5 引入了更丰富的正则表达式引擎,因此它是 Java、PHP、Python 等中的标准。由于 Perl 有助于支持
sed
语法的子集,因此您可能可以转换一个简单的sed< /code> 脚本到 Perl,以使用此扩展正则表达式方言中的有用功能,例如否定断言:
将用
not
替换不是str
的字符串。(?:...)
是一个非捕获组(与许多sed
方言不同,未转义的括号是 Perl 中的元字符)和(? !str)
是一个否定断言;为了使正则表达式匹配,字符串中该位置之后的文本不得为str
。+
重复此模式,直到无法匹配为止。请注意,断言在匹配中的每个位置都需要为真,因此我们一次使用.
匹配一个字符(新手经常会犯这个错误,并且错误地仅在较长的开头断言)模式,但是它可能与内部某处的str
匹配,从而导致“泄漏”)。There are two possible interpretations of your question. Like others have already pointed out,
[^x]
matches a single character which is notx
. But an empty string also isn'tx
, so perhaps you are looking for[^x]\|^$
.Neither of these answers extend to multi-character sequences, which is usually what people are looking for. You could painstakingly build something like
to compose a regular expression which doesn't match
str
, but a much more straightforward solution insed
is to delete any line which does matchstr
, then keep the rest;Perl 5 introduced a much richer regex engine, which is hence standard in Java, PHP, Python, etc. Because Perl helpfully supports a subset of
sed
syntax, you could probably convert a simplesed
script to Perl to get to use a useful feature from this extended regex dialect, such as negative assertions:will replace a string which is not
str
withnot
. The(?:...)
is a non-capturing group (unlike in manysed
dialects, an unescaped parenthesis is a metacharacter in Perl) and(?!str)
is a negative assertion; the text immediately after this position in the string mustn't bestr
in order for the regex to match. The+
repeats this pattern until it fails to match. Notice how the assertion needs to be true at every position in the match, so we match one character at a time with.
(newbies often get this wrong, and erroneously only assert at e.g. the beginning of a longer pattern, which could however matchstr
somewhere within, leading to a "leak").除了提供的所有答案之外,您还可以使用符号
[^:[C_CLASS]:]
否定 sed 中的字符类,例如[^[:blank:]]
将匹配任何不被视为空格字符的内容。In addition to all the provided answers , you can negate a character class in sed , using the notation
[^:[C_CLASS]:]
, for example ,[^[:blank:]]
will match anything which is not considered a space character .根据我自己的经验,并且下面的帖子支持这一点,sed 不支持使用“^”的正常正则表达式否定。我不认为 sed 有直接否定方法...但是如果您检查下面的帖子,您会看到一些解决方法。
Sed 正则表达式和子字符串否定
From my own experience, and the below post supports this, sed doesn't support normal regex negation using "^". I don't think sed has a direct negation method...but if you check the below post, you'll see some workarounds.
Sed regex and substring negation