包含文件路径的lldb正则表达式

时间:2013-09-03 19:07:59

标签: regex nsstring escaping lldb

我正在尝试编写一个将图像写入文件的lldb别名。以下工作,但有一个问题:

command regex logImageFile 's/(.+) (.+)/ expr (int) [ (id)UIImagePNGRepresentation(%1) writeToFile: @"%2.png" atomically: YES ]/'

问题是我每次都必须输入完整路径。拥有一个总是使用的目录对我来说会好得多。所以我尝试了这个:

command regex logImageFile 's/(.+) (.+)/ expr (int) [ (id)UIImagePNGRepresentation(%1) writeToFile: @"/users/myUsername/Desktop/tempImages/%2.png" atomically: YES ]/'

现在,当我在Xcode控制台中输入如下内容时,lldb总是说logImageFile不是有效命令。

logImageFile fooImage barFile

问题可能是路径内的斜线。我想我不得不以某种方式逃脱它们,但是怎么样?请注意,我所拥有的是lldb正则表达式中的NSString - 但我不知道实际上是什么样的正则表达式。

1 个答案:

答案 0 :(得分:2)

我认为问题在于您使用/字符来分隔正则表达式的各个部分,并且该字符也会出现在文件路径中的替换文本中。最简单的解决方法是使用不同的分隔符。 s///格式是最常见的,但您可以轻松使用s###。 e.g。

(lldb) command regex logImageFile 's#(.+) (.+)#expr (int) puts("[ (id)UIImagePNGRepresentation(%1) writeToFile: @\"/users/myUsername/Desktop/tempImages/%2.png\" atomically: YES ]")#'
(lldb) logImageFile fooImage barFile
(int) $1 = 10
[ (id)UIImagePNGRepresentation(fooImage) writeToFile: @"/users/myUsername/Desktop/tempImages/barFile.png" atomically: YES ]

我告诉expr这里的返回类型为(int),因此它正在打印(将其分配给便捷变量$1) - 但如果您使用(void) ,它可以避免打印任何东西。

我在这里使用puts()而不是您尝试设置的真实调用 - 但我认为我发现了原始正则表达式命令别名的问题。试一试。