Skip to content
shellhacks.dev
Regular Expressions

Match a whole word

Match a whole word with the GNU boundary escape, the -w option or the angle-bracket word anchors. All three treat the low line as a word character.

1 min read
Summarize in AI

Searching for cat finds concat too, and you want only the word on its own.

Shell
words=$(printf '%s\n' cat concat cat_food cat-food 'the cat sat')
echo "$words" | grep -n '\bcat\b'
echo ---
echo "$words" | grep -nw cat
echo ---
echo "$words" | grep -n '\<cat\>'
echo ---
echo "$words" | grep -nP '(?<![A-Za-z])cat(?![A-Za-z])'
Text
1:cat
4:cat-food
5:the cat sat
---
1:cat
4:cat-food
5:the cat sat
---
1:cat
4:cat-food
5:the cat sat
---
1:cat
3:cat_food
4:cat-food
5:the cat sat

A word boundary is a position between a word character, a letter, a digit or a low line, and a non-word character. The first three forms are equivalent in GNU grep, so cat_food is rejected because the low line counts as part of its word, while cat-food is accepted. The lookaround pattern in the last block defines a word as letters only and so accepts cat_food as well. The anchors are described in the GNU grep manual.

The boundary escape and the angle-bracket anchors are extensions of GNU grep and sed and are not in POSIX. BSD and macOS spell them with bracketed word-start and word-end classes, while -w is portable. Digits are word characters too, so cat2 does not match, and letters outside ASCII depend on the locale.

Taggedgrepregex
Share

More snippets

Regular Expressions

Match a UUID

Match a version 4 UUID with 8-4-4-4-12 hex groups, a 4 at the start of the third group and 8, 9, a or b at the start of the fourth. Add -i for uppercase.

2 min read
Regular Expressions

Match an ISO date

Match an ISO date with [0-9]{4}-[0-9]{2}-[0-9]{2}. The pattern checks the shape only, and a tighter month and day range still accepts February 31.

2 min read
Regular Expressions

Match a semantic version

Match a semantic version with the pattern from semver.org, which runs in grep -E. It rejects leading zeros, a bare 1.2 and a v prefix, and takes pre-release tags.

1 min read

Get new posts by email

One email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.