无法使用正则表达式与grep / sed一起工作

时间:2017-06-13 01:00:33

标签: regex bash shell sed grep

我一直在使用脚本来使用构建脚本使用新值更新pbxproj文件中的PRODUCT_BUNDLE_IDENTIFIER。我提出的正则表达式选择了&PRODUCTS_BUNDLE_IDENTIFIER ='之间的所有内容。以及最多2次出现'的任何文字。'这就是我想要的。

这里显示的正则表达式找到了这些出现的位置:

(?<=PRODUCT_BUNDLE_IDENTIFIER = )([a-zA-Z0-9_]+(?:\.[a-zA-Z0-9_]+){0,2})

我已在此处使用验证码对其进行了测试:https://regex101.com/r/jUhJm7/1

为了节省时间,这里是应用了正则表达式的屏幕截图,并根据需要选择了绿色部分,因此正则表达式似乎正在工作,并按预期识别以下示例的包ID部分: Regex Validated

我遇到的问题是,当使用这个正则表达式与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并且感觉我只是未能正确地将各个部分放在一起。

1 个答案:

答案 0 :(得分:2)

尝试使用GNU sed(使用<div class="col-md-4"> <ul class="list-unstyled"> <li><strong>England</strong></li> <li> <div class="checkbox"> <label> <input type="checkbox"> Chelsea </label> </div> </li> <li> <div class="checkbox"> <label> <input type="checkbox"> Mu </label> </div> </li> <li> <div class="checkbox"> <label> <input type="checkbox"> Arsenal </label> </div> </li> </ul> </div> <div class="col-md-4"> <ul class="list-unstyled"> <li><strong>Spain</strong></li> <li> <div class="checkbox"> <label> <input type="checkbox"> Madrid </label> </div> </li> <li> <div class="checkbox"> <label> <input type="checkbox"> Barcelona </label> </div> </li> <li> <div class="checkbox"> <label> <input type="checkbox"> Atletico </label> </div> </li> </ul> </div>进行unix):

-E

例如:

$ sed -r "s/(PRODUCT_BUNDLE_IDENTIFIER = )[a-zA-Z0-9_]+(\.[a-zA-Z0-9_]+){0,2}/\1${BUNDLE_ID}/"
相关问题