代码之家  ›  专栏  ›  技术社区  ›  mles

正则表达式与文件路径中的名称不匹配

  •  1
  • mles  · 技术社区  · 9 年前

    我有一个包含ipa文件的文件夹。我需要通过一个 appstore enterprise

    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 ?

    2 回复  |  直到 9 年前
        1
  •  1
  •   Inian    9 年前

    你把 字符串匹配与正则表达式匹配。对于贪婪的全局匹配,例如 * 您只需将测试运算符用于 == ,

    #!/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
    

    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
    

    .* 像

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

    也就是说要符合你最初的要求 enterprise 或 appstore bash

    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
    
        2
  •  1
  •   Tushar    9 年前

    for i in build-test/*; do if [[ $i =~ appstore|enterprise ]]; then echo $i; fi; done
    
    推荐文章