0

I have a word list with a lot of words that I want to convert into a json file. To do this I need to make them strings and add a comma in the end of all words. I figured it would be nice to use regular expression to do this and I know about replace-regexp. What I have tried to do this, beginning with the file:

This 
Is 
An 
Example

To match each word I use ^([A-Za-z]+)$. I have tried it in https://regex101.com/ and it seems to work the way as expected. But how do I actually use these matches to add a " in front and back of each word to make strings of them?

"This"
"Is"
"An"
"Example"

I can not find in the documentation how to actually use the matches to add more characters.

Edit: After reading https://www.emacswiki.org/emacs/RegularExpression and experimenting with regex101 I found that the words is group 1, so "\1", (added a comma just to complete the json structure) should work, but it does not

Edit2: Just a more clear example of one of the most promising tries: replace-regexp <RET> ^([A-Za-z]+)$ <RET> "\1",

2
  • Please consider showing the code you've tried so far. That way, someone can perhaps help with that code or offer another suggestion.
    – Drew
    Aug 8, 2018 at 1:40
  • Sorry I fought I have, will be more specific
    – Salviati
    Aug 8, 2018 at 2:14

2 Answers 2

1

(and ) are ordinary characters in Emacs RegExp, you need to escape them with a / to make them a group, thus you should try

M-x replace-regexp <RET> ^\([A-Za-z]+\)$ <RET> "\1", <RET>

You can also use

M-x replace-regexp <RET> ^[A-Za-z]+$ <RET> "\&", <RET>

where \& stands for the whole match.

0
2

You can just read the file in, split it into lines, and then encode directly to json like this:

(require 'json)

(with-temp-file "words.json"
  (insert (with-temp-buffer (insert-file-contents "words.txt")
                (json-encode
                 (split-string (buffer-string) "\n" t)))))

This gives me a json file with these contents.:

["This","Is","An","Example"]
3
  • Great example, but it doesn't really answer the question/request, which was about using replace-regexp. Not sure how much OP wanted to solve the problem vs how much OP wanted to learn about regexps and replace-regexp.
    – Drew
    Aug 8, 2018 at 4:33
  • The answer works well, thank you! But like Drew said I really would like to find a way of doing it with replace-regexp just so I can use it in the future.
    – Salviati
    Aug 8, 2018 at 4:49
  • This is almost an XY problem. You mentioned you need to do X and focused on Y to solve it. But there is a much better way than Y to solve X, and still many issues with Y. For example, what happens if your words have unicode characters in them? Or a space? Or a dash? You might try this instead: Replace regexp: ^.+$ -> "\&", That will wrap spaces too, but handle the other cases. probably some regex guru can figure out how to trim these off. Aug 8, 2018 at 15:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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