我想知道是否可以检测随机字符串末尾的数字。例如,在这个字符串中:“localfile/random/1”我想分割这个字符串,这样一个变量将是字符串末尾的数字,第二个变量将是路径的其余部分。我无法通过谷歌搜索任何内容。
任何帮助将不胜感激。
最佳答案
如果您可以将最后一个斜杠周围的字符串分开:
set x to "localfile/random/1"
set text item delimiters to "/"
{(text items 1 thru -2 of x) as text, last text item of x}
只有当字符串始终以数字结尾时,这两种方法才有效:
on test(x)
set c to count of x
repeat with i from 1 to c
if item (c - i) of x is not in items of "0123456789" then
return {text 1 thru (c - i) of x, text (c - i + 1) thru -1 of x}
end if
end repeat
end test
test("localfile/random/1")
do shell script "sed -E $'s/([0-9]*)$/\\\\\\n\\\\1/' <<< " & quoted form of "localfile/random/1"
set {x, y} to paragraphs of result
关于numbers - 使用 Applescript 将字符串的结束数字分隔到变量中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15602456/