2

我想知道是否可以检测到随机字符串末尾的数字。例如在这个字符串中:“localfile/random/1”我想分割这个字符串,所以一个变量将是字符串末尾的数字,第二个变量将是路径的其余部分。我无法谷歌任何东西。

任何帮助将不胜感激。

4

3 回答 3

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
于 2013-03-25T16:33:28.897 回答
1

这将允许您拆分字符串,而无需在数字字符串之前指定最后一个字符:

set myString to "localfile/random/13451"
set delimiter to "||"

set {TID, text item delimiters} to {text item delimiters, delimiter}
set myItems to text items of (do shell script "sed -E 's/([0-9]+$)/" & delimiter & "\\1/' <<< " & quoted form of myString)
set text item delimiters to TID
return myItems 
于 2013-03-25T20:20:34.823 回答
0

这个怎么样?

set s to "blah4"
set l to (get last character of s)
if "1234567890" contains l then log "yes"
于 2014-07-04T12:18:56.190 回答