我一直在使用腳本來使用構建腳本使用新值更新pbxproj文件中的PRODUCT_BUNDLE_IDENTIFIER。我提出的正則表達式選擇'PRODUCT_BUNDLE_IDENTIFIER ='和任意文本之間的所有內容,最多可以發生2次'。'。這是我想要的。無法使用正則表達式來使用grep/sed
我已經把找到這些OCCURENCES正則表達式如下所示:
(?<=PRODUCT_BUNDLE_IDENTIFIER =)([a-zA-Z0-9_]+(?:\.[a-zA-Z0-9_]+){0,2})
我已經與驗證這裏測試它:https://regex101.com/r/jUhJm7/1
爲了節省時間,這裏是用截圖應用正則表達式並根據需要選擇綠色部分,因此正則表達式似乎正在工作,並且按照預期識別以下示例的bundle id部分:
我遇到的問題當使用grep,grep -e,egrep或sed使用這個正則表達式時,它似乎沒有以相同的方式工作。我想用sed來運行字符串替換,並嘗試下面的方法來實現這一目標:
# variable definitions
BUNDLE_ID='mynew.bundle.id'
PBXFILE="$SRCROOT/myproject.xcodeproj/project.pbxproj"
# check if the test bundle id is currently in the file
if grep -Fq "REPLACEABLE_BUNDLE_ID" $PBXFILE; then
# this commented version works as expected as it's using simple string replacement
#sed -i '' "s/REPLACEABLE_BUNDLE_ID/$BUNDLE_ID/g" $PBXFILE
# these are the versions of the regex I've tried with sed #
# basic version working in validator & testing with sublime text regex engine
(?<=PRODUCT_BUNDLE_IDENTIFIER =)([a-zA-Z0-9_]+(?:\.[a-zA-Z0-9_]+){0,2})
# added extra parentheses around product id first portion
(?<=(PRODUCT_BUNDLE_IDENTIFIER =))([a-zA-Z0-9_]+(?:\.[a-zA-Z0-9_]+){0,2})
# escaped version
\(?<=PRODUCT_BUNDLE_IDENTIFIER = \)\([a-zA-Z0-9_]+\(?:\.[a-zA-Z0-9_]+\){0,2}\)
# try replacing the current bundle id using the regex
sed -i -E '' "s/I put the regex here/$BUNDLE_ID/g" $PBXFILE
fi
我用正則表達式相當新的,之前沒有使用SED。我在這裏閱讀了關於擴展正則表達式:http://www.grymoire.com/Unix/Regular.html#uh-12,覺得我只是沒有把這些東西放在一起。
不同的工具支持正則表達式的不同口味... sed的支持BRE/ERE它沒有按」沒有lookarounds或未命名的捕獲組等...使用'perl'而不是..或使用捕獲組和反向引用...建議刪除圖像和regex101相關的東西從問題..只需添加示例文本(無圖像)並輸出爲 – Sundeep
@孫德普感謝您的意見,我得到了sed工作但我最終可能會用perl或ruby來做這件事。 :) – digitalHound