Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I extract a captured Gnu emacs regex (regexp) group to insert elsewhere?

In Gnu Emacs (I'm running V 24.3), I can capture a matching group like this:

M-x re-search-forward
RE search: \([0-9]+\)\.txt

... which finds a string with one or more digits followed by .txt. But now I'd like to insert the string of digits (captured in group 1) in another buffer.

I'm guessing the answer is obvious, but it's eluding me. Any hints?

like image 265
fearless_fool Avatar asked Oct 31 '22 06:10

fearless_fool


1 Answers

Use (match-string 1), to get the string matching the 1st subgroup.

This assumes that you have not changed the match data (e.g. by doing another search) since you did re-search-forward.

See the Elisp manual, node Simple Match Data.

like image 56
Drew Avatar answered Nov 15 '22 11:11

Drew