gpt4 book ai didi

regex - grep 正则表达式开始于,结束于但不包含

转载 作者:太空狗 更新时间:2023-10-29 11:27:20 26 4
gpt4 key购买 nike

我在 Linux 的文本文件中使用 grep 和 sed 命令。我正忙于处理 Documents/Data 文件夹和 share/dict/words 文档。我需要提取单词文件中以“q”开头、以“s”结尾但不包含“a”或“r”的所有单词。到目前为止,我已经设法获得了这段代码:

grep –n ‘\<q.*[ar]s\>’ /usr/share/dict/words

这给了我不应该包含的词。我试过添加一个“|”然后 grep -v 排除如下这些词:

grep –n ‘\<q.*s\>’ /usr/share/dict/words | grep –nv ‘\<q.*[ar]s\>’ /usr/share/dict/words

这只会返回不以“q”开头或以“s”结尾的所有其他单词

最佳答案

I need to extract all words within the words file that begin with "q", end with "s" but does not contain "a" or "r".

您需要使用否定的 character class :

grep -n "\<q[^ar]*s\>" /usr/share/dict/words

您可能还想引用 Regular expressions .

关于regex - grep 正则表达式开始于,结束于但不包含,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23888396/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com