Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Syntax of emacs replace-regexp

Tags:

regex

emacs

I'm trying to find every date and put quotes around it. I thought this should be:

M-x replace-regexp 201\d-\d\d-\d\d <ret> '\&'

I also tried [0-9] instead of \d.

it doesn't work. But using isearch-forward-regexp I can type [0-9][0-9] and watch the targets highlight. What am I doing wrong with the replace?

like image 939
Dov Avatar asked Apr 05 '11 19:04

Dov


2 Answers

Emacs regexps don't have the common \d shorthand for [0-9].

I just put the text 2011-04-01 into a new buffer, went back to the start of the buffer, and typed M-x replace-regexp RET 201[0-9]-[0-9][0-9]-[0-9][0-9] RET '\&' RET, and the date was surrounded by single quotes, as expected.

like image 123
Sean Avatar answered Oct 05 '22 17:10

Sean


\d is not supported in Emacs regular expression syntax (C-h S regexp has documentation on them). You can use [0-9] as you did, or use the POSIX style [[:digit:]].

However, what you did (with [0-9]) should have worked, and did in fact just work for me. If you're using a regular expression in a program you might also find M-x regexp-builder useful.

like image 28
Nicholas Riley Avatar answered Oct 05 '22 17:10

Nicholas Riley