热门标签 | 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.

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


推荐阅读
  • Python正则表达式学习记录及常用方法
    本文记录了学习Python正则表达式的过程,介绍了re模块的常用方法re.search,并解释了rawstring的作用。正则表达式是一种方便检查字符串匹配模式的工具,通过本文的学习可以掌握Python中使用正则表达式的基本方法。 ... [详细]
  • Python爬虫中使用正则表达式的方法和注意事项
    本文介绍了在Python爬虫中使用正则表达式的方法和注意事项。首先解释了爬虫的四个主要步骤,并强调了正则表达式在数据处理中的重要性。然后详细介绍了正则表达式的概念和用法,包括检索、替换和过滤文本的功能。同时提到了re模块是Python内置的用于处理正则表达式的模块,并给出了使用正则表达式时需要注意的特殊字符转义和原始字符串的用法。通过本文的学习,读者可以掌握在Python爬虫中使用正则表达式的技巧和方法。 ... [详细]
  • ALTERTABLE通过更改、添加、除去列和约束,或者通过启用或禁用约束和触发器来更改表的定义。语法ALTERTABLEtable{[ALTERCOLUMNcolu ... [详细]
  • 先看官方文档TheJavaTutorialshavebeenwrittenforJDK8.Examplesandpracticesdescribedinthispagedontta ... [详细]
  • 本文介绍了在处理不规则数据时如何使用Python自动提取文本中的时间日期,包括使用dateutil.parser模块统一日期字符串格式和使用datefinder模块提取日期。同时,还介绍了一段使用正则表达式的代码,可以支持中文日期和一些特殊的时间识别,例如'2012年12月12日'、'3小时前'、'在2012/12/13哈哈'等。 ... [详细]
  • 本文详细介绍了Python中正则表达式和re模块的使用方法。首先解释了转义符的作用,以及如何在字符串中包含特殊字符。然后介绍了re模块的功能和常用方法。通过学习本文,读者可以掌握正则表达式的基本概念和使用技巧,进一步提高Python编程能力。 ... [详细]
  • 本文整理了315道Python基础题目及答案,帮助读者检验学习成果。文章介绍了学习Python的途径、Python与其他编程语言的对比、解释型和编译型编程语言的简述、Python解释器的种类和特点、位和字节的关系、以及至少5个PEP8规范。对于想要检验自己学习成果的读者,这些题目将是一个不错的选择。请注意,答案在视频中,本文不提供答案。 ... [详细]
  • 本文记录了作者对x265开源代码的实现与框架进行学习与探索的过程,包括x265的下载地址与参考资料,以及在Win7 32 bit PC、VS2010平台上的安装与配置步骤。 ... [详细]
  • Ihaveaworkfolderdirectory.我有一个工作文件夹目录。holderDir.glob(*)>holder[ProjectOne, ... [详细]
  • 本文介绍了lua语言中闭包的特性及其在模式匹配、日期处理、编译和模块化等方面的应用。lua中的闭包是严格遵循词法定界的第一类值,函数可以作为变量自由传递,也可以作为参数传递给其他函数。这些特性使得lua语言具有极大的灵活性,为程序开发带来了便利。 ... [详细]
  • PHP图片截取方法及应用实例
    本文介绍了使用PHP动态切割JPEG图片的方法,并提供了应用实例,包括截取视频图、提取文章内容中的图片地址、裁切图片等问题。详细介绍了相关的PHP函数和参数的使用,以及图片切割的具体步骤。同时,还提供了一些注意事项和优化建议。通过本文的学习,读者可以掌握PHP图片截取的技巧,实现自己的需求。 ... [详细]
  • 本文讨论了一个关于正则的困惑,即为什么一个函数会获取parent下所有的节点。同时提出了问题是否是正则表达式写错了。 ... [详细]
  • Excel数据处理中的七个查询匹配函数详解
    本文介绍了Excel数据处理中的七个查询匹配函数,以vlookup函数为例进行了详细讲解。通过示例和语法解释,说明了vlookup函数的用法和参数的含义,帮助读者更好地理解和运用查询匹配函数进行数据处理。 ... [详细]
  • Android开发实现的计时器功能示例
    本文分享了Android开发实现的计时器功能示例,包括效果图、布局和按钮的使用。通过使用Chronometer控件,可以实现计时器功能。该示例适用于Android平台,供开发者参考。 ... [详细]
  • 本文介绍了Oracle存储过程的基本语法和写法示例,同时还介绍了已命名的系统异常的产生原因。 ... [详细]
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社区 版权所有