【问题标题】:Regex doesn't match in bash, but works in online Regex tool [duplicate]正则表达式在 bash 中不匹配,但适用于在线正则表达式工具 [重复]
【发布时间】:2017-09-06 07:39:37
【问题描述】:

我需要在 xCode 构建期间替换 swift 代码中的一些值。 我的快速代码是这样的:

struct KeyConstants {
    static let uniqueKeyName = "some key"
}

我添加了一个构建阶段并编写了一些脚本来用新值替换旧值:

NEW_VALUE=$"new value"
find . -type f -name 'MyFile.swift' -exec sed -i '' s/"uniqueKeyName = \"(.*?)\""/"uniqueKeyName = \"${NEW_VALUE}\""/ {} +

我认为问题在于正则表达式,它在某些在线正则表达式工具中有效,但在 bash 中无效。

sed "s/uniqueKeyName = \"(.*?)\"/uniqueKeyName = \"new value\"/g" file.txt

请帮忙,提前谢谢。

【问题讨论】:

标签: ios xcode bash macos


【解决方案1】:

我从this post 找到了解决方案。使用"[^"]*" 而不是"(.*?)"

find ${FILE_PATH} -type f -name '*.swift' -exec sed -i '' "s/uniqueKeyName = \"[^\"]*\"/uniqueKeyName = \"${NEW_VALUE}\"/" {} +

【讨论】:

    猜你喜欢
    • 2018-11-13
    • 2012-07-24
    • 1970-01-01
    • 2016-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多