【问题标题】:lldb regex that includes a file path包含文件路径的 lldb 正则表达式
【发布时间】:2013-09-03 19:07:59
【问题描述】:

我正在尝试编写一个将图像写入文件的 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 ——但我不知道它实际上是什么风格的正则表达式。

【问题讨论】:

    标签: regex nsstring escaping lldb


    【解决方案1】:

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

    (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(),而不是您尝试设置的真正调用 - 但我想我发现了您原来的正则表达式命令别名的问题。试试这个。

    【讨论】:

    • 如此简单的解决方案,我不知道我尝试了多久。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-03
    • 2014-08-03
    • 2011-04-18
    • 2017-11-08
    相关资源
    最近更新 更多