I'm almost completely ignorant in elisp, but I'd like to have a function that does the following:
Takes a string from (a) a region, if one is selected, or (b) user prompt, if a region is not selected.
Internally replaces a couple of regexp in the string with other strings. (Specifically, first "[^a-zA-Z_- ]" with "", and then "[_ ]+" with "_").
Saves the new string obtained from the replacement into the kill-ring or the clipboard.
Note that the original region is not changed.
My starting point was the piece of elisp below, taken from snips I've seen, to take care of 1.. Please note that I barely understand what this code actually does:
defun changestring (&optional arg)
"Replace some character in string and save to kill-ring."
(interactive "p")
(if (use-region-p)
(let ((region (buffer-substring-no-properties (region-beginning) (region-end))))
*** the replacing part should go here ***
)
(let ((region (read-string "String to be converted: ")))
*** the replacing part should go here, again ***
)
)
)
I've been told that it can be slimmed down – indeed it looks like the replacement part is done twice.
Stackexchange seems to have some answers about saving to kill-ring, e.g. this and this, and modifying strings internally, e.g. this and this, but I really don't understand how to modify the code in those answers to this particular problem.
I wonder if anyone can help me, at least with all parts besides the regexp part, which I think I can modify myself. Of course a comment of your code is also greatly appreciated, so I can learn some elisp. Cheers!