热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

用xcode中的“(换行){”替换“{”的正则表达式-RegularExpressiontoreplace“{”with“(newline){”inxcode

Ineedtochangemycodingstyleofputtingopeningbracesinsamelinetonewline.Ineedtofind

I need to change my coding style of putting opening braces in same line to new line. I need to find and replace the (space){ with (newline){. I heard using regular expression find and replace, its pretty simple.

我需要改变我的编码风格,把开括号放在同一行到新行。我需要找到并替换(空格){和(换行){。我听到使用正则表达式查找和替换,它非常简单。

Could anyone help me on this?

有人能帮我吗?

5 个解决方案

#1


47  

You could try the following:

你可以试试下面的方法:

  • In the Find box, type space \ { $
  • 在“查找”框中,输入空格\ {$
  • In the Replace box, type control+q return {
  • 在替换框中,类型控制+q返回{

control+q is needed to quote the return key. There’s no visual feedback for typing control+q return, so the only visible character in the replace box is the opening curly brace:

需要控件+q来引用返回键。输入控制+q返回没有视觉反馈,所以替换框中唯一可见的字符是左大括号:

Screenshot Find & Replace

Although this answers your question, there’s (at least!) one problem: it won’t indent the opening curly brace, so something like

虽然这回答了你的问题,但至少有一个问题:它不会缩进大括号,所以类似。

- (void)method {
    for (obj in collection) {
        NSLog(@"%@", obj);
    }
}

is converted to

被转换为

- (void)method
{
    for (obj in collection)
{
        NSLog(@"%@", obj);
    }
}

The menu item Edit > Format > Re-Indent will place the opening curly braces in the correct indentation tab but there might be non-desired side effects to your code style.

菜单项编辑>格式> reindent将在正确的缩进选项卡中放置左花括号,但您的代码风格可能有不希望的副作用。


Edit: as commented in the other answer, you might want a regular expression that matches an arbitrary number of whitespaces surrounding the curly brace, e.g. \s*{\s*$

编辑:正如在另一个答案中评论的那样,您可能想要一个正则表达式,该表达式与围绕花括号的任意数目的空格匹配,例如:s*{s \s*$

#2


2  

search for this \s\{ and replace with \n\{

搜索这个\s\{并替换为\n\{

Your editor needs to support regular expressions in both search and replace fields. If you can't use the \n in the replace dialog because it takes the string literally, try a option-enter followed by {, that works in most editors I tried.

编辑器需要在搜索和替换字段中支持正则表达式。如果您不能在替换对话框中使用\n,因为它从字面上理解了字符串,请尝试一个选项-enter,然后是{,在我尝试过的大多数编辑器中工作。

  • the \s is a space character (if there could be more spaces you can use \s+)
  • \s是一个空格字符(如果有更多的空格可以使用\s+)

note it has to be \s+ instead of \s* as someone fixed because indeed \s* also matches in case there is no space.

注意,它必须是\s+而不是\s*作为一个固定的,因为确实\s*也匹配,以防没有空格。

  • the \{ needs the backslash because {
    needs to be escaped as it has another meaning in a regex
  • \{需要反斜杠,因为{需要转义,因为它在regex中有另一个含义
  • the \n is for a newline
  • \n表示换行

The best way however would be to reformat your code where you choose to have your { on a new line. Most editors allow you to set these options.

但是,最好的方法是重新格式化您选择在新行中使用{的代码。大多数编辑器允许您设置这些选项。

Another way is to use a code beautifier, you can google these online and some allow to change settings like that.

另一种方法是使用代码美化器,您可以在网上谷歌这些和一些允许改变这样的设置。

#3


1  

You can use [\n\r] to describe newline It's a shame, I came across with that when I tap on magnifier icon next to search bar while finding something in a single file.

你可以用[\n\r]来描述换行,真可惜,当我点击搜索栏旁边的放大镜图标时,在一个文件中发现了一些东西。

To see several other expressions:

查看其他几个表达式:

  1. go to a file
  2. 去一个文件
  3. do cmd+f and open search
  4. 做cmd+f和开放搜索
  5. tap on the magnifier icon at the left-hand side of search field at the top
  6. 点击搜索框左边的放大镜图标
  7. you can see other expressions in the dropdown menu
  8. 您可以在下拉菜单中看到其他表达式

#4


0  

i searched a while to find out, that you have to enable the find options for regular expressions first on the small magnifier glass in the left side of the find-input field ;)

我搜索了一会儿才发现,您必须首先在find-input字段左侧的小放大镜上启用正则表达式的查找选项;

#5


0  

Just copy an example of needed replacement string (new line or else) from code to replacement box.

只需将所需的替换字符串(新行或其他)从代码复制到替换框。


推荐阅读
author-avatar
1403390367_f5c8a8
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有