Search code examples
ripgrep

search start/end of word with ripgrep


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?


Solution

  • 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.