Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

search start/end of word with ripgrep

Tags:

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?

like image 217
pseyfert Avatar asked Jan 18 '19 13:01

pseyfert


People also ask

What is the best way to search for patterns on ripgrep?

(Nearly what you'd find in ripgrep's man page, so pipe it into a pager!) -i/--ignore-case: When searching for a pattern, ignore case differences. That is rg -i fast matches fast, fASt, FAST, etc.

What is ripgrep in Linux?

Ripgrep is a command line tools that searches patterns under your current directory, like the good old grep, but with faster speed. In this post, I list some of the commonly-used flags for ripgrep.

How do I search the contents of a folder using ripgrep?

Here, ripgrep limited its search to the src directory. Another way of doing this search would be to cd into the src directory and simply use rg 'fn write\ (' again. After recursive search, ripgrep's most important feature is what it doesn't search.

Does ripgrep work with recursive searches?

In this case, the recursive searches results will not consider binary files and hidden files/directories. ripgrep shares a DNA footprint with popular search tools like grep, find, ack, and The Silver Searcher.


1 Answers

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.
like image 85
BurntSushi5 Avatar answered Sep 22 '22 13:09

BurntSushi5