Hyphen/Dash to be included in regex word boundary \b
Asked Answered
S

1

6

Simply put:

echo "xxxxx Tyyy zzzzz" | egrep "\byyy\b" 

(no match which is correct)

echo "xxxxx T-yyy zzzzz" | egrep "\byyy\b" 
xxxxx T-yyy zzzzz

I dont want it to match like it does in the second expression, please advise how I can achieve this, thanks.

Somnus answered 3/9, 2015 at 15:56 Comment(0)
F
5

You can use:

echo "xxxxx T-yyy zzzzz" | grep -E "(^|[^-])\byyy\b([^-]|$)"

Where (^|[^-])\byyy\b([^-]|$) will match start or non-hyphen on LHS and end or non-hyphen on RHS of the matched word yyy.

Filip answered 3/9, 2015 at 16:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.