如何在多行文本块上使用Xcode的“查找和替换" [英] How can I use Xcode's `Find and Replace` on Multiline blocks of text

查看:123
本文介绍了如何在多行文本块上使用Xcode的“查找和替换"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个大文件,其中包含数百个文本块(C ++方法调用的一部分),类似于下面的Current块,我希望使用 Xcode的正则表达式替换将每个文本更改为类似于下面的Desired块,捕获值0.89f,一个名称,0.1、0.5和2.

请注意,两个块分别以new Zapp//d.p.’s to show开头和结尾,因此,为节省自己的手动编辑工作量,我希望使用正则表达式搜索和替换.

我面临的主要问题是积木的multi-line性质.如果我们暂时忽略它,我将执行ALMOST搜索并替换以下single-line块:

当前:

new Zapp (0.89f // defaultParameterValue , "A name" // name , 0.1 // min , 0.5 // max , 2 // d.p.'s to show

所需:

new Zapp (NormalisableRange<float> (0.1, 0.5) ,NormalisableRange<float> (0.1, 0.5).convertTo0to1(0.89f) , "A name" // name , 2 // d.p.'s to show

查找并替换:

查找:Zapp\s+\((.*)\s+// defaultParameterValue\s+,\s+"(.*)"\s+\/\/\s+name\s+,\s+(.*)\s+\/\/\s+min\s+,\s+(.*)\s+\/\/\s+max\s+,\s+(.*)\s+\/\/\s+d.p.'s

替换:Zapp (NormalisableRange<float> ($3, $4) ,NormalisableRange<float> ($3, $4).convertTo0to1($1) , "$2" // name , $5 // d.p.’s

单行解决方案存在以下问题:捕获1(1个空格),捕获3(4个空格)和捕获5(3个空格)之后会看到其他空白,否则它将起作用好吧:

我尝试使用[^\s]+从捕获1(0.89f),捕获3(0.1)和捕获5(2)中消除空白,但这无济于事,因此如果有人可以指出原因,我d感激不尽.

我可以用\s+搜索upperlower,但是我不能将它们全部组合在一起以达到期望的结果.

我在网上看到一些老帖子,暗示使用Xcode可能无法完成此任务.如果是这样,那么有人可以建议一种替代方法来完成这项工作吗?

解决方案

原因是.*过于贪婪,无法匹配\s+模式(但对于此贪婪子模式)可以使用的空格. >

要么使用懒惰/不愿意的.*?:

Zapp\s+\((.*?)\s+// defaultParameterValue\s+,\s+"(.*)"\s+\/\/\s+name\s+,\s+(.*?)\s+\/\/\s+min\s+,\s+(.*?)\s+\/\/\s+max\s+,\s+(.*?)\s+\/\/\s+d.p.'s

请参见 regex演示

或定义用于匹配浮点数/整数[-+\w.]+的类(有点不精确,但由于期望值类型已知,所以可以这样做)或双引号文字("([^"\\]*(?:\\.[^"\\]*)*)"):

Zapp\s+\(([-+\w.]+)\s+// defaultParameterValue\s+,\s+"([^"\\]*(?:\\.[^"\\]*)*)"\s+\/\/\s+name\s+,\s+([-+\w.]+)\s+\/\/\s+min\s+,\s+([-+\w.]+)\s+\/\/\s+max\s+,\s+([-+\d.]+)\s+\/\/\s+d.p.'s

请参见另一个演示

I have a large file with hundreds of blocks of text (parts of C++ method calls) that resemble the Current block below and I wish to use Xcode’s Regular Expression replacement to change each one to resemble the Desired block below, capturing values 0.89f, A name, 0.1, 0.5 and 2.

Note that both blocks start and end with new Zapp and //d.p.’s to show respectively so to save myself loads of manual editing I wish to use regex search and replace.

The main problem I face is the multi-line nature of the blocks. If we ignore that for a moment, I have ALMOST performed a search and replace on the following single-line blocks:

Current:

new Zapp (0.89f // defaultParameterValue , "A name" // name , 0.1 // min , 0.5 // max , 2 // d.p.'s to show

Desired:

new Zapp (NormalisableRange<float> (0.1, 0.5) ,NormalisableRange<float> (0.1, 0.5).convertTo0to1(0.89f) , "A name" // name , 2 // d.p.'s to show

The Find and Replace:

Find: Zapp\s+\((.*)\s+// defaultParameterValue\s+,\s+"(.*)"\s+\/\/\s+name\s+,\s+(.*)\s+\/\/\s+min\s+,\s+(.*)\s+\/\/\s+max\s+,\s+(.*)\s+\/\/\s+d.p.'s

Replace: Zapp (NormalisableRange<float> ($3, $4) ,NormalisableRange<float> ($3, $4).convertTo0to1($1) , "$2" // name , $5 // d.p.’s

This single-line solution has the problem that additional white-space is seen after capture 1 (1 space), capture 3 (4 spaces) and capture 5 (3 spaces), otherwise it works well:

I tried to eliminate the whitespace from capture 1 (0.89f), capture 3 (0.1) and capture 5 (2) using [^\s]+ but it didn't help so if anybody can point out the reason for that I'd be grateful.

I can, for what it's worth, search for upper and lower using \s+ but I can't put it all together to achieve the desired result.

I've seen some old posts online that suggest this task might not be possible with Xcode. If that is true, can anybody suggest an alternative approach that will get the job done?

解决方案

The reason is the .* is too greedy and match the whitespace that could have been matched by the \s+ pattern but for this greedy subpattern.

Either use the lazy/reluctant .*?:

Zapp\s+\((.*?)\s+// defaultParameterValue\s+,\s+"(.*)"\s+\/\/\s+name\s+,\s+(.*?)\s+\/\/\s+min\s+,\s+(.*?)\s+\/\/\s+max\s+,\s+(.*?)\s+\/\/\s+d.p.'s

See the regex demo

Or define the class for matching floats/integers [-+\w.]+ (a bit not precise, but will do since the expected value type is known) or double quoted literals ("([^"\\]*(?:\\.[^"\\]*)*)"):

Zapp\s+\(([-+\w.]+)\s+// defaultParameterValue\s+,\s+"([^"\\]*(?:\\.[^"\\]*)*)"\s+\/\/\s+name\s+,\s+([-+\w.]+)\s+\/\/\s+min\s+,\s+([-+\w.]+)\s+\/\/\s+max\s+,\s+([-+\d.]+)\s+\/\/\s+d.p.'s

See another demo

这篇关于如何在多行文本块上使用Xcode的“查找和替换"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆