search start/end of word with ripgrep
Asked Answered
L

1

32

With grep I can search for the start and end of a word with

grep -e '\<leg\>' <where to search>

this would find I have a leg. but not play allegro here.

Ripgrep (0.10.0) does not seem to support this way of writing this regular expression. My question thus is:

How to "grep" for occurrences at the begin/end of a word with ripgrep?

Lotte answered 18/1, 2019 at 13:18 Comment(0)
F
51

ripgrep doesn't support the \< and \> word boundaries, which specifically only match the start and end of a word, respectively. ripgrep does however support \b, which matches a word boundary anywhere. In this case, it's good enough for your specific example:

$ echo 'play allegro here' | rg '\bleg\b'
$ echo 'I have a leg.' | rg '\bleg\b'
I have a leg.    

ripgrep also supports grep's -w flag, which effectively does the same thing in this case:

$ echo 'play allegro here' | rg -w leg
$ echo 'I have a leg.' | rg -w leg
I have a leg.
Fimbria answered 18/1, 2019 at 21:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.