I want to capitalize letters that follow colons. Here is the code:

(replace-regexp-in-string ": \w" 'upcase ": really")

The result I expected is : Really. Instead I get : really.

Similarly, this also doesn't work:

(replace-regexp-in-string ": [:alpha:]" 'upcase ": really")

But this works:

(replace-regexp-in-string ": r" 'upcase ": really")

And this, too:

(replace-regexp-in-string ": [:alpha:]" 'upcase ": alright")

My LANG environment variable is set to en_US.UTF-8.

  • 2
    In the first example you are missing a backslash and in the second a set of braces. I.e. these regexps don't match the provided string.
    – politza
    Jan 30 '15 at 1:09
  • 2
    And I'm pretty sure your last example didn't work ☺
    – Malabarba
    Jan 30 '15 at 1:42
  • @Malabarba, there was an error in the example. I fixed it.
    – tmalsburg
    Jan 30 '15 at 5:42

You need to double the backslash in a Lisp string:

(replace-regexp-in-string ": \\w" #'upcase ": really")

See the Elisp manual, node Syntax for Strings.

And a char class has chars inside [], so put [:alpha:] inside [].

(replace-regexp-in-string ": [[:alpha:]]" #'upcase ": really")

See the Elisp manual, node Regexp Special.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.