ios 使用正则表达式在 Xcode 中查找/替换

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4778847/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 18:38:44  来源:igfitidea点击:

Find/Replace in Xcode using Regular Expression

iphoneiosobjective-cxcode

提问by EmptyStack

I have the following function calls at several places in my class.

我在班级的几个地方有以下函数调用。

[myClass doOperationOne];
[myClass doOperationTwo];
[myClass doOperationThree];

In those lines, I want to search for the following,

在这些行中,我想搜索以下内容,

[myClass doOperationOne
[myClass doOperationTwo
[myClass doOperationThree

And replace them with the following, (by appending WithOptions:[Permissions isOptionsAvailable])

并用以下内容替换它们,(通过附加WithOptions:[Permissions isOptionsAvailable]

[myClass doOperationOneWithOptions:[Permissions isOptionsAvailable]];
[myClass doOperationTwoWithOptions:[Permissions isOptionsAvailable]];
[myClass doOperationThreeWithOptions:[Permissions isOptionsAvailable]];

How can I do this using single Regular Expression Find/Replace?

如何使用单个正则表达式查找/替换来执行此操作?

采纳答案by EmptyStack

Somehow I've managed to find the answer (which is enough for my need here) by referring the post: http://www.cocoabuilder.com/archive/xcode/273123-how-to-use-regular-expressions-in-xcode-find-replace.html, and trial and error method.

不知何故,我设法通过参考帖子找到了答案(这足以满足我的需要):http: //www.cocoabuilder.com/archive/xcode/273123-how-to-use-regular-expressions-in -xcode-find-replace.html和试错法。

My Findstring is:

我的查找字符串是:

(\[myClass.myMethod )(.*)(\];)

And, my Replacestring is:

而且,我的替换字符串是:

WithOptions:[Permissions isOptionsAvailable]

Please post if there is any better way than this..

如果有比这更好的方法,请发布..

回答by Kalle

NOTE: The behavior changed in Xcode 6. The \123 syntax was replaced with $123. Also be warned that newlines can now be matched in reg exps so be sure to skim before Replace-Alling

注意:Xcode 6 中的行为发生了变化。 \123 语法被替换为 $123。另请注意,现在可以在 reg exps 中匹配换行符,因此请务必在 Replace-Alling 之前略读

Adding an additional argument to a method:

向方法添加额外的参数:

To replace

取代

[* setFoo:*]

with

[* setFoo:* bar:value]

you do

你做

(setFoo:)(.*)(\])
 bar:value]

(search string and replacement string respectively).

(分别是搜索字符串和替换字符串)。

or, if on Xcode 5 or older

或者,如果使用 Xcode 5 或更早版本

(setFoo:)(.*)(\])
 bar:value]

(below uses new syntax; swap if on Xcode 5 or older)

(以下使用新语法;如果使用 Xcode 5 或更旧版本,请交换)

NOTE: Everything written here applies to the string-replacement methods in NSString/NSMutableStringwhen using NSRegularExpressionSearch!

注意:这里写的所有内容都适用于NSString/ 中的字符串替换方法,NSMutableString当使用NSRegularExpressionSearch!

In other words, this line:

换句话说,这一行:

[input replaceOccurrencesOfString:@"\n\[([^\]^\n]*)\]\n" 
                       withString:@"\n///\n\n[]\n" 
                          options:NSRegularExpressionSearch
                            range:(NSRange){0,input.length}];

will convert all "[...]\n"sequences (leaving e.g. "[...\n..."alone!) into "\n///\n\n[...]\n", preserving ...using $1.

将所有"[...]\n"序列(例如"[...\n..."单独留下!)转换为"\n///\n\n[...]\n",...使用 $1保留。

Always did this by hand but in this case, I was adding an OPTIONAL 'animate:' flag, and the default up to this point had been YES, but I wanted NO, so every call had to be updated.

总是手动执行此操作,但在这种情况下,我添加了一个可选的“animate:”标志,到目前为止的默认值是 YES,但我想要 NO,因此每次调用都必须更新。

More examples:

更多例子:

Deprecated methods (iOS)

弃用的方法 (iOS)

dismissModalViewControllerAnimated:...deprecation

dismissModalViewControllerAnimated:...弃用

To fix the deprecated dismissModalViewControllerreplacing it with an empty completion block and retaining animated value:

要修复已弃用的dismissModalViewController替换为空的完成块并保留动画值的问题:

(dismissModalViewControllerAnimated:)(.*)(\])
dismissViewControllerAnimated: completion:nil]


presentModalViewController:animated:deprecation

presentModalViewController:animated:弃用

(presentModalViewController:)(.*)( animated:)(.*)(\])
presentViewController: completion:nil]

Miscellaneous

各种各样的

PD...Release → PD...Destroy

PD...发布 → PD...销毁

I recently wrote a c library with a bunch of files with the prefix PDand I used Create/Release as malloc/free keywords, which I regretted as it may make people think retain counting is kept, so I wanted to renamePD<anything>Release(with PD<anything>Destroy(.

我最近用一堆带有前缀的文件编写了 ac 库,PD我使用 Create/Release 作为 malloc/free 关键字,我很遗憾,因为它可能让人们认为保留计数被保留,所以我想PD<anything>Release(PD<anything>Destroy(.

([\n\r ])(PD)(.*)Release\(
Destroy(

Since Core Graphics has CGPDFDocumentReleaseand similar, I had to ensure the word startedwith PD as well.

由于核芯显卡具有CGPDFDocumentRelease相似,我必须确保这个词开始与PD为好。



PDAssert(PDScannerPop...(...));

PDAssert(PDScannerPop...(...));

I had stupidly put assertions around functional code that would become empty when !#ifdef DEBUG. Luckily I knew that all of these started with PDAssert(PDScannerPop.... and ended with );.

我愚蠢地在函数代码周围放置了断言,当 !#ifdef DEBUG 时这些代码会变为空。幸运的是,我知道所有这些都以 PDAssert(PDScannerPop.... 并以 ); 结尾。

(PDAssert\()(PDScannerPop)(.*)(\);)
;

No $1 here because that would include the PDAssert(again. Note that I've split right after the PDAssert( and am leaving out the ) in ); in the 3rd chunk which removes the surplus parens from removing PDAssert(.

这里没有 1 美元,因为那将包括PDAssert(再次。请注意,我在 PDAssert( 并且在 ) 中省略了 ) 之后立即拆分;在从删除 PDAssert(.

Dealing with end parentheses

处理结束括号

You can match everything except ")" to deal with over-greedy regexp replaces. Example:

您可以匹配除“)”之外的所有内容来处理过度贪婪的正则表达式替换。例子:

foo(replace(arg), bar)
foo(newvalue(newarg), bar)

Using replace\((.*)\)will grab replace(arg), bar)and result will be foo(newvalue(newarg)! Instead use replace\(([^\)]*)\)which will grab replace(arg)and leave , bar)alone.

使用replace\((.*)\)将抓取replace(arg), bar),结果将是foo(newvalue(newarg)!而是使用replace\(([^\)]*)\)which 将抓住replace(arg)并离开, bar)

Converting a bunch of NSString properties from using retain and/or strong (and whatever else) to using copy

将一堆 NSString 属性从使用保留和/或强(以及其他任何东西)转换为使用复制

@property \(([^\)]*)[sr][te][rt][oa][ni][gn]([^\)]*)\)(.*)NSString(.*)
@property (copy)NSString

The weird sr te rt thing in the center matches both "strong" and "retain".

中心奇怪的 sr te rt 东西与“强”和“保留”都匹配。

回答by Suragch

Note for Xcode 8

Xcode 8 的注意事项

I was trying to update my answer below for Xcode 8, but I couldn't do it. If you have the answer, please let me know.

我试图在下面为 Xcode 8 更新我的答案,但我做不到。如果你有答案,请告诉我。

Xcode 7

Xcode 7

Unless you were already a regular expression genius, this much easier to do in Xcode now. You don't need to use regular expressions at all (but you can if you still want to).

除非您已经是正则表达式天才,否则现在在 Xcode 中更容易做到这一点。您根本不需要使用正则表达式(但如果您仍然想要,可以使用)。

Example

例子

Replace these strings:

替换这些字符串:

[myClass doOperationOne];
[myClass doOperationTwo];
[myClass doOperationThree];

[myClass doOperation];
[myClass doOperation];
[myClass doOperation];

with these strings

用这些字符串

[myClass doOperationOneWithOptions:[Permissions isOptionsAvailable]];
[myClass doOperationTwoWithOptions:[Permissions isOptionsAvailable]];
[myClass doOperationThreeWithOptions:[Permissions isOptionsAvailable]];

[myClass doOperation OneWithOptions:[Permissions isOptionsAvailable]];
[myClass doOperation 2WithOptions:[Permissions isOptionsAvailable]];
[myClass doOperationWithOptions:[Permissions isOptionsAvailable]];

The bolded numbers show why a plain Find and Replace won't work.

粗体数字显示了为什么简单的查找和替换不起作用。

Setup

设置

There are two ways you can do Find and Replace: Textualand Regular Expression

有两种方法可以进行查找和替换:文本正则表达式

Click the magnifying glass

点击放大镜

enter image description here

在此处输入图片说明

and "Edit Find Options..."

和“编辑查找选项...”

enter image description here

在此处输入图片说明

and choose one of them.

并选择其中之一。

enter image description here

在此处输入图片说明

Method 1 - Textual

方法 1 - 文本

  • Enter [myClass doOperationin Find

  • Click the magnifying class and choose "Insert Pattern"

  • 输入[myClass doOperation查找

  • 单击放大类并选择“插入图案”

enter image description here

在此处输入图片说明

  • Choose "Any Word Characters"
  • 选择“任意单词字符”

enter image description here

在此处输入图片说明

  • Add ];to Find
  • 添加];到查找

enter image description here

在此处输入图片说明

  • Repeat for the Replacement String but use WithOptions:[Permissions isOptionsAvailable]];for the last part.
  • 对替换字符串重复,但WithOptions:[Permissions isOptionsAvailable]];用于最后一部分。

enter image description here

在此处输入图片说明

  • A single Replace All will work now.
  • 单个替换全部现在将起作用。

Method 2 - Regular Expression

方法 2 - 正则表达式

  • Make sure that Regular Expressions are selected as described in Setup above.

  • Use (\[myClass doOperation)(.*)(\];)for the Regular Expression

  • Use $1$2WithOptions:[Permissions isOptionsAvailable]$3for the Replacement String. The $1, $2, and $3replace the parentheses in the regular expression.

  • 确保按照上述设置中的说明选择正则表达式。

  • 使用(\[myClass doOperation)(.*)(\];)正则表达式

  • 使用$1$2WithOptions:[Permissions isOptionsAvailable]$3了替换字符串。的$1$2$3替换正则表达式的括号内。

enter image description here

在此处输入图片说明

  • A single Replace All will work now.

  • See this linkfor more help with regular expressions.

  • 单个替换全部现在将起作用。

  • 有关正则表达式的更多帮助,请参阅此链接

See also

也可以看看

回答by griotspeak

I can't find anything saying that regular expression capture and replace is supported WITHIN Xcode.

我找不到任何说明在 Xcode 中支持正则表达式捕获和替换的内容。

you would want

你会想要

\[myClass\.myMethod\sdoOperation([A-Z][a-z]+)\] 

to capture the number. though. that is what the parenthesis are for.

来捕捉号码。尽管。这就是括号的用途。

回答by jakev

In the current version of Xcode, I believe you can only search using regular expressions. Replace doesn't give you that flexibility. If you only have three methods you want to replace in this manner, I would run search and replace three times. Otherwise, I would modify my source code in BASH using awk or sed.

在当前版本的 Xcode 中,我相信您只能使用正则表达式进行搜索。替换不会给你那种灵活性。如果您只想以这种方式替换三种方法,我会运行搜索和替换三遍。否则,我将使用 awk 或 sed 在 BASH 中修改我的源代码。