简体   繁体   English

Emacs正则表达式查询替换,删除了表达式中的空间

[英]Emacs regex query replace, space in expression is removed

I am trying to replace an erroneous C bracing style in a file with regexes in emacs. 我试图用emacs中的正则表达式替换文件中的错误C支撑样式。

All 所有

if (foo)
{
    bar();
}

and the likes must become: 并且喜欢必须变成:

if (foo) {
    bar();
}

Please let's not discuss whether it's a good idea to do that, or which bracing style is best. 不要讨论这样做是一个好主意,还是哪种支撑风格是最好的。 Please. 请。

I am trying to achieve that with emacs' query-replace-regexp : 我正在尝试通过emacs的query-replace-regexp实现这一目标:

  • match " ^Q^J +{ " 匹配“ ^Q^J +{
  • replace with " SPC{^Q^J ", with SPC being an actual space (not showing here on SO) 替换为“ SPC{^Q^J ”,其中SPC是实际空间(SO此处未显示)

Somehow, the first character in the string to replace to is not taken into account, and I end up with: 不知何故,没有考虑要替换为字符串中的第一个字符,我最终得到:

if (foo){
    bar();
}

(no space before { .) {之前没有空格。)

Any idea what is happening, and how I can make sure the space gets there? 知道发生了什么,如何确保空间到达那里?

How about this: 这个怎么样:

(defun braces-KR-style ()
  (interactive)
  (goto-char (point-min))
  (while (search-forward-regexp "\n[[:blank:]]*?{" nil t)
    (replace-match " {" nil t))
  )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM