sed:我的模式可以包含“不是”吗?特点?我怎么说“不是X”?

发布于 2024-12-06 02:03:59 字数 39 浏览 3 评论 0原文

如何在 sed 中说“不是”某个字符?

How do I say "is not" a certain character in sed?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

时常饿 2024-12-13 02:03:59
[^x]

这是一个接受除 x 之外的任何字符的字符类。

[^x]

This is a character class that accepts any character except x.

落叶缤纷 2024-12-13 02:03:59

对于那些对所选答案不满意的人,按照约翰尼的评论

su[^x] 将匹配 sumsun,但不匹配 su

您可以使用以下语法告诉 sed notx 匹配行:

sed '/x/! s/su//' file

查看 kkeller 的答案再举一个例子。

For those not satisfied with the selected answer as per johnny's comment.

su[^x] will match sum and sun but not su.

You can tell sed to not match lines with x using the syntax below:

sed '/x/! s/su//' file

See kkeller's answer for another example.

泪之魂 2024-12-13 02:03:59

您的问题有两种可能的解释。就像其他人已经指出的那样,[^x] 匹配不是 x 的单个字符。但空字符串也不是 x,因此您可能正在寻找 [^x]\|^$

这些答案都不能扩展到多字符序列,而这通常是人们正在寻找的。您可以煞费苦心地构建一些类似于

[^s]\|s\($\|[^t]\|t\($\|[^r]\)\)\)

编写与 str 不匹配的正则表达式的东西,但 sed 中更直接的解决方案是删除匹配的任何行 匹配str,然后保留其余部分;

sed '/str/d' file

Perl 5 引入了更丰富的正则表达式引擎,因此它是 Java、PHP、Python 等中的标准。由于 Perl 有助于支持 sed 语法的子集,因此您可能可以转换一个简单的 sed< /code> 脚本到 Perl,以使用此扩展正则表达式方言中的有用功能,例如否定断言:

perl -pe 's/(?:(?!str).)+/not/' file

将用 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 not x. But an empty string also isn't x, 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

[^s]\|s\($\|[^t]\|t\($\|[^r]\)\)\)

to compose a regular expression which doesn't match str, but a much more straightforward solution in sed is to delete any line which does match str, then keep the rest;

sed '/str/d' file

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 simple sed script to Perl to get to use a useful feature from this extended regex dialect, such as negative assertions:

perl -pe 's/(?:(?!str).)+/not/' file

will replace a string which is not str with not. The (?:...) is a non-capturing group (unlike in many sed 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 be str 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 match str somewhere within, leading to a "leak").

扮仙女 2024-12-13 02:03:59

除了提供的所有答案之外,您还可以使用符号 [^:[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 .

过度放纵 2024-12-13 02:03:59

根据我自己的经验,并且下面的帖子支持这一点,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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文