is there something akin to regEx in applescript, and if not, what's the alternative?

前端 未结 7 710
感动是毒
感动是毒 2020-12-04 15:54

I need to parse the first 10 chars of a file name to see if they are all digits. The obvious way to do this is fileName =~ m/^\\d{10}/ but I\'m not seeing anything regExy i

7条回答
  •  再見小時候
    2020-12-04 16:17

    I'm sure there is an Applescript Addition or a shell script that can be called to bring regex into the fold, but I avoid dependencies for the simple stuff. I use this style pattern all the time...

    set filename to "1234567890abcdefghijkl"
    
    return isPrefixGood(filename)
    
    on isPrefixGood(filename) --returns boolean
        set legalCharacters to {"1", "2", "3", "4", "5", "6", "7", "8", "9", "0"}
    
        set thePrefix to (characters 1 thru 10) of filename as text
    
        set badPrefix to false
    
        repeat with thisChr from 1 to (get count of characters in thePrefix)
            set theChr to character thisChr of thePrefix
            if theChr is not in legalCharacters then
                set badPrefix to true
            end if
        end repeat
    
        if badPrefix is true then
            return "bad prefix"
        end if
    
        return "good prefix"
    end isPrefixGood
    

提交回复
热议问题