reddit / emacs

Use "\/\*(.|\n)*?\*\/" with replace-regexp-in-string

ShadowRylander · 2022-02-08 22:02
Hello! I'd like to remove comments in a [minified css stylesheet](https://github.com/shadowrylander/settings/blob/main/src/styles/highlight/gruvbox-dark-hard.css), and to do so, I'm curling the stylesheet and trying to regex replace the comments with `\/\*(.|\n)*?\*\/`; however, while it works with a regex tester, it doesn't work in `replace-regexp-in-string`. I've noticed that I have to escape the `\*`, the brackets, and the newline character, but is there any way to escape whatever is needed, without me having to remember to translate a regular regexp to an elisp regexp? Thank you kindly for the help!
SlowValue · 2022-02-09 07:47
It is possible. I would use `M-x re-builder` to do that. In re-builder, first switch it to `string` syntax mode (`M-x reb-change-syntax <RET> string <RET>`. Then paste your regexp between those `"`. Next switch to `read` syntax mode. A good thing with `re-builder` is, that you can see if your regexp matches in the buffer. `re-builder` is also able to handle Perl style regexps and `rx` style regexps, if you install some packages.
7890yuiop · 2022-02-09 09:06
> ... a regex tester What regexp tester? Did it use the Emacs dialect of regexps? [No, we can see that it did not.] You can't use any arbitrary regexp tool with any arbitrary implementation of regular expressions; they need to speak the same language. https://www.gnu.org/software/emacs/manual/html_node/emacs/Regexps.html https://emacs.stackexchange.com/q/55601 might help too. > trying to regex replace the comments with \/\*(.|\n)*?\*\/ I'm guessing you want this: "/\\*\\(.\\|\n\\)*?\\*/" Interactively that would be: /\*\(.\| \)*?\*/
7890yuiop · 2022-02-09 09:06
> `M-x reb-change-syntax <RET> string <RET>` `C-c TAB` > `re-builder` is also able to handle ... `rx` style regexps, if you install some packages. Also if you don't.
ShadowRylander · 2022-02-09 10:08
Thanks for pointing me in the direction of `re-builder`; I'll be sure to check it out!
ShadowRylander · 2022-02-09 10:13
I was using [regexer](https://regexr.com/), but yep; that did the trick! I'll be sure to use `re-builder` in the future, then, and thanks for the links as well!
w08r · 2022-02-09 20:24
I really like visual-regexp [1] and the steroids add on [2] for an alternative way to visually try regex replacements and for choosing different regex interpreters respectively. [1] https://github.com/benma/visual-regexp.el [2] https://github.com/benma/visual-regexp-steroids.el/
ShadowRylander · 2022-02-09 20:28
Thanks for the packages; I'll be sure to use them the next time I work with regexps, especially the second one!
SlowValue · 2022-02-11 20:30
>> M-x reb-change-syntax <RET> string <RET> > > C-c TAB Your shortcut isn't complete, right? Also sometimes I prefer to give function names.