sd icon indicating copy to clipboard operation
sd copied to clipboard

Replace with a literal dollar symbol followed by another character

Open tyilo opened this issue 4 years ago • 5 comments

I want to replace \w+ with $a.

Nothing seems to work using latest git version (4ccc52203afda466a409f1b0835798ebdb2ee622):

$ echo foo | sd '\w+' '$a'

$ echo foo | sd '\w+' '\$a'
\
$ echo foo | sd '\w+' '\\$a'
\
$ echo foo | sd '\w+' '\\\$a'
\
$ echo foo | sd '\w+' '\\\\$a'
\
$ echo foo | sd '\w+' '\\\\\$a'
\\\

Using string mode works, but then I can't use regex in the search:

$ echo foo | sd -s 'foo' '$a'
$a

tyilo avatar May 18 '21 12:05 tyilo

$ echo foo | sd '\w+' '$$a'
$a
  • You need to use \w+ to match one or more word characters. w+ will match one or more w characters.
    • Edit: just noticed that you mention \w+ in the first line, I commented based on code samples which uses w+ only.
  • Use $$ to indicate literal $ character in replacement section.

learnbyexample avatar May 18 '21 13:05 learnbyexample

  • Use $$ to indicate literal $ character in replacement section.

Ah, could this be documented in --help?

tyilo avatar May 18 '21 14:05 tyilo

Just checked the help page and seems it isn't documented. I knew about $$ from using ripgrep. Perhaps you could submit the PR.

learnbyexample avatar May 18 '21 15:05 learnbyexample

sd and ripgrep both use rust's regex implementation, so anything there is supported here:

https://docs.rs/regex/latest/regex/

balupton avatar Dec 02 '21 06:12 balupton

Created a PR to document it in the readme.

ruudk avatar May 22 '22 18:05 ruudk