匹配单词开头和结尾的单词 [英] Match word starting with and ending

查看:193
本文介绍了匹配单词开头和结尾的单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个grep表达式,该表达式匹配任何以"h"开头和以"d"结尾的单词.因此,它应该返回"head","heard","hammered"之类的单词

I'm trying to write a grep expression that matches any word starting with "h" and ending with "d". So it should return words like "head" "heard" "hammered"

尝试了此表达式,但未返回任何内容.

Tried this expression but it returned nothing.

grep -o '^h\w*d\b' file.txt

推荐答案

您的表达式基本上是正确的,但是^匹配输入的开头-可能不是您想要的.您可能想用\b代替^,以匹配开头的单词边界.

Your expression is mostly correct, however ^ matches the beginning of input -- that's likely not what you want. You probably want to substitute a \b for the ^ to match a word boundary at the beginning.

下面的表达式应该起作用.

The following expression should work.

grep -o '\bh\w*d\b' file.txt

这篇关于匹配单词开头和结尾的单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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