I am trying to do a search and replace with regexs.
Suppose I have a foreach(foo1.txt, foo2.txt, foo3.txt, foo4.txt)
.
And I want to put " around each item in the list.
I thought- from the documentation - that this regex would work
(foo[1-4]\.txt) -> "\&"
.
But it doesn't. What am I doing wrong?
Note that brackets and parentheses need escaping ("extra slashes") in a search regexp, but not in the replace regexp.
The escapes are required because the lisp-parser is seeing the regex prior to the regex engine.
But, if you're writing programmatically (as opposed to interactive entry), you need to escape the escapes! ay-yi-yi....
interactive:
M-x replace-regexp RET \(obj.method(.*)\{1\}\) RET trim$(\1)
programmatic:
(replace-regexp "\\(obj.method(.*)\\{1\\}\\)" "trim$(\\1)")
the escaped parens are escaped because they are grouping, the unescaped parens are literal parens.
You might want to check out the Emacs Wiki Regex Crash Course and Steve Yegge's Effective Emacs regex section.
Try something like this \(foo[1-4]\.txt\)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With