gpt4 book ai didi

applescript - AppleScript:清洁字符串

转载 作者:行者123 更新时间:2023-12-01 09:18:07 28 4
gpt4 key购买 nike

我有一个字符串,其中包含要删除的非法字符,但我不知道可能存在哪种字符。

我建立了一个我不希望被过滤的字符列表,并建立了这个脚本(来自我在网络上找到的另一个脚本)。

on clean_string(TheString)
--Store the current TIDs. To be polite to other scripts.
set previousDelimiter to AppleScript's text item delimiters
set potentialName to TheString
set legalName to {}
set legalCharacters to {"a", "b", "c", "d", "e", "f",
"g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r",
"s", "t", "u", "v", "w", "x", "y", "z", "A", "B", "C", "D", "E",
"F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R",
"S", "T", "U", "V", "W", "X", "Y", "Z", "1", "2", "3", "4", "5",
"6", "7", "8", "9", "0", "?", "+", "-", "Ç", "ç", "á", "Á", "é",
"É", "í", "Í", "ó", "Ó", "ú", "Ú", "â", "Â", "ã", "Ã", "ñ", "Ñ",
"õ", "Õ", "à", "À", "è", "È", "ü", "Ü", "ö", "Ö", "!", "$", "%",
"/", "(", ")", "&", "€", "#", "@", "=", "*", "+", "-", ",", ".",
"–", "_", " ", ":", ";", ASCII character 10, ASCII character 13}

--Whatever you want to eliminate.
--Now iterate through the characters checking them.
repeat with thisCharacter in the characters of potentialName
set thisCharacter to thisCharacter as text
if thisCharacter is in legalCharacters then
set the end of legalName to thisCharacter
log (legalName as string)

end if
end repeat
--Make sure that you set the TIDs before making the
--list of characters into a string.
set AppleScript's text item delimiters to ""
--Check the name's length.
if length of legalName is greater than 32 then
set legalName to items 1 thru 32 of legalName as text
else
set legalName to legalName as text
end if
--Restore the current TIDs. To be polite to other scripts.
set AppleScript's text item delimiters to previousDelimiter
return legalName
end clean_string

问题在于该脚本运行缓慢,给我超时。

我正在做的是逐个字符地检查并与LegalCharacters列表进行比较。如果角色在那里,那很好。如果没有,请忽略。

有快速的方法吗?

就像是

“查看TheString的每个字符,并删除legalCharacters上没有的那些字符”



谢谢你的帮助。

最佳答案

您遇到了哪些非ASCII字符?您的文件编码是什么?

使用Shell脚本和tr,sed或perl处理文本的效率要高得多。默认情况下,所有语言都安装在OS X中。

您可以使用带有tr的shell脚本(如下例所示)剥离返回值,也可以使用sed剥离空格(不在以下示例中):

set clean_text to do shell script "echo " & quoted form of the_string & "| tr -d '\\r\\n' "

Technical Note TN2065: do shell script in AppleScript

或者,使用perl,这将去除非打印字符:
set x to quoted form of "Sample text. smdm#$%%&"
set y to do shell script "echo " & x & " | perl -pe 's/[^[:alnum:]|[:space:]]//g'"

在SO周围搜索其他示例,这些示例使用tr,sed和perl通过Applescript处理文本。或搜索 MacScripter / AppleScript | Forums

关于applescript - AppleScript:清洁字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2783713/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com