2017-07-28 18 views
1

私はipaファイルを持つフォルダを持っています。ファイル名にappstoreまたはenterpriseという文字列を付けることで、それらを特定する必要があります。Regexがファイルパスの名前と一致しません

mles:drive-ios-swift mles$ ls build 
com.project.drive-appstore.ipa         
com.project.test.swift.dev-enterprise.ipa 
com.project.drive_v2.6.0._20170728_1156.ipa      

私が試した:

#!/bin/bash -veE 

fileNameRegex="**appstore**" 

for appFile in build-test/*{.ipa,.apk}; do 
if [[ $appFile =~ $fileNameRegex ]]; then 
    echo "$appFile Matches" 
else 
    echo "$appFile Does not match" 
fi 
done 

しかし何も一致しません:

mles:drive-ios-swift mles$ ./test.sh 
build-test/com.project.drive-appstore.ipa Does not match 
build-test/com.project.drive_v2.6.0._20170728_1156.ipa Does not match 
build-test/com.project.test.swift.dev-enterprise.ipa Does not match 
build-test/*.apk Does not match 

どのように正しいスクリプトがbuild-test/com.project.drive-appstore.ipaにマッチするように見えるのでしょうか?

+0

「 fileNameRegex = "。* appstore。*" '。 – Phylogenesis

答えて

1

グロブ文字列と正規表現のマッチを混同しています。 *のような貪欲なグロブの試合のためにあなただけの==でテスト演算子を使用することができ、

#!/usr/bin/env bash 

fileNameGlob='*appstore*' 
#   ^^^^^^^^^^^^ Single quote the regex string 

for appFile in build-test/*{.ipa,.apk}; do   
    # To skip non-existent files 
    [[ -e $appFile ]] || continue 

    if [[ $appFile == *${fileNameGlob}* ]]; then 
     echo "$appFile Matches" 
    else 
     echo "$appFile Does not match" 
    fi 
done 

.*

fileNameRegex='.*appstore.*' 
if [[ $appFile =~ ${fileNameRegex} ]]; then 
    # rest of the code 

として正規表現を使用貪欲試合で結果

build-test/com.project.drive_v2.6.0._20170728_1156.ipa Does not match 
build-test/com.project.drive-appstore.ipa Matches 
build-test/com.project.test.swift.dev-enterprise.ipa Does not match 

(または)を生成


あなたの元の要件をマグロブを使用してbash

にグロブの一致を拡張ファイル名の使用中のTCH enterpriseまたはappstore文字列:

shopt -s nullglob 
shopt -s extglob 
fileExtGlob='*+(enterprise|appstore)*' 

if [[ $appFile == ${fileExtGlob} ]]; then 
    # rest of the code 

と正規表現と、

fileNameRegex2='enterprise|appstore' 
if [[ $appFile =~ ${fileNameRegex2} ]]; then 
    # rest of the code 
+1

存在しないファイルをスキップする方法を教えてくれてありがとう。非常に役立つ! – mles

1

あなたは、AppStoreのと企業を一致させるために、次の正規表現を使用することができますファイル名:

for i in build-test/*; do if [[ $i =~ appstore|enterprise ]]; then echo $i; fi; done 
関連する問題