2

I was recently trying to convert exported Common Lisp symbols from strings to keywords. I basically wanted "HELLO" to become :hello. I tried using the Emacs replace-regexp command. Here are the two expressions I gave it:

"\([^ ]*\)"
:\,(downcase \1)

I tested the first one by checking if it would find all of the cases I wanted it to replace and it did. However, when I went through with the replacement, it worked except the symbols were not lowercase ("HELLO" became :HELLO). I then tried changing the first expression into a case by case one. Something along the lines of:

"\([-A-Z+*/\<>=&]*\)"

Oddly enough when I used the same second expression with this new first expression, it worked perfectly. Is this a bug, or am I actually doing something wrong?

npostavs
  • 4,877
  • 1
  • 24
  • 43
malisper
  • 1,671
  • 1
  • 12
  • 16
  • I couldn't make it work on regex101.com but take a look at regex conversion http://www.regular-expressions.info/replacecase.html – Federico Piazza Aug 09 '14 at 23:56
  • 1
    See #2 of http://stackoverflow.com/a/13706627/319698 – npostavs Aug 09 '14 at 23:58
  • npostavs, you are right, thank you. It is just such a weird default. @Fede, you need to run it in Emacs since the replacement expression has elisp code in it. – malisper Aug 10 '14 at 00:16

1 Answers1

2

This is the documented behaviour of replace-regexp, see its docstring:

Preserve case in each match if `case-replace' and `case-fold-search' are non-nil and REGEXP has no uppercase letters.

So to avoid case preservation, you may either include upper case letters in the matching regexp, or M-x set-variable RET case-replace RET nil. (You could also set case-fold-search, but that affects a lot of searching functions, whereas case-replace only affects replacing functions).

See https://stackoverflow.com/a/13706627/319698 for the non-interactive version of this.

Community
  • 1
  • 1
npostavs
  • 4,877
  • 1
  • 24
  • 43