☰
-
Php 95
-
Java 94
-
Javascript 37
-
Regex 18
-
Git 17
-
Security 16
-
Docker 07
-
Python 58
-
Machine Learning 14
-
Book Highlights
Regex Grep Word Boundaries
Metasequences \b, word boundaries We can also use \< \> grep '\b(cat)\b' -i Cat category // cat
Word boundaries
p15 A regex that match a word can also match it embedded within a larger word.
#!/bin/sh
: "Word boundaries, a common problem:
A regex that match a word can also match it embedded
Use \b and \b
Use \< and \>
"
A='gray grayscale'
B='cat category'
C='doggy dog'
echo $A | grep 'gray' -o | tee result.txt
echo $B | grep '\b(cat)\b' -E -o | tee result.txt -a
echo $C | grep '\<dog\>' -E -o | tee result.txt -a

gray
gray
cat
dog
➥ Questions
Last update: 57 days ago