gpt4 book ai didi

regex - 使用 grep 查找带有通配符和排除项的整个单词

转载 作者:行者123 更新时间:2023-12-04 10:19:32 24 4
gpt4 key购买 nike

我有一些随机字符串。我正在尝试仅使用以下内容打印整个单词:

grep -ioh "\w*ice\w*"

这工作正常,但似乎不显示符号,而只显示字母。我希望通配符允许除空格之外的任何符号。这是可能的吗?

为了更好地解释,上面的代码在一个字符串中,如:

猫很nice 只显示“nice”,而我想要“very-nice”

最佳答案

您可以使用

grep -ioh "\S*ice\S*"

或者
grep -ioh "[^[:space:]]*ice[^[:space:]]*"
[^[:space:]] (或 \S )匹配除空白字符以外的任何字符。

grep online demo :
grep -ioh "\S*ice\S*" <<< "The cat is very-nice"
## => very-nice

关于regex - 使用 grep 查找带有通配符和排除项的整个单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60926862/

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