在 xcode 中用“(换行符){”替换“{”的正则表达式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5078814/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Regular Expression to replace " {" with "(newline){" in xcode
提问by Aaron
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?
有人可以帮我吗?
回答by
You could try the following:
您可以尝试以下操作:
- In the Find box, type space\{$
- In the Replace box, type control+qreturn{
- 在查找框中,键入 space\{$
- 在替换框中,键入 control+qreturn{
control+qis needed to quote the return key. There's no visual feedback for typing control+qreturn, so the only visible character in the replace box is the opening curly brace:
control+q需要引用返回键。输入没有视觉反馈control+qreturn,因此替换框中唯一可见的字符是左花括号:
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.
菜单项 Edit > Format > Re-Indent 会将左花括号放置在正确的缩进选项卡中,但可能会对您的代码样式产生不希望的副作用。
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*$
回答by Stofke
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,因为它从字面上接受字符串,请尝试使用 option-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 - 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.
另一种方法是使用代码美化器,您可以在线谷歌这些,有些允许更改设置。
回答by landonandrey
Just copy an example of needed replacement string (new line or else) from code to replacement box.
只需将所需替换字符串(新行或其他)的示例从代码复制到替换框。
回答by Mert Buran
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]
来描述newline
这是一种耻辱,当我在单个文件中查找某些内容时点击搜索栏旁边的放大镜图标时,我遇到了这一点。
To see several other expressions:
要查看其他几个表达式:
- go to a file
- do
cmd
+f
and open search - tap on the magnifier icon at the left-hand side of search field at the top
- you can see other expressions in the dropdown menu
- 转到文件
- 做
cmd
+f
并打开搜索 - 点击顶部搜索字段左侧的放大镜图标
- 您可以在下拉菜单中看到其他表达式
回答by rubo77
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 ;)
我搜索了一段时间才发现,您必须首先在查找输入字段左侧的小放大镜上启用正则表达式的查找选项;)