Reputation: 21
I was wondering if it's possible to detect a number on the end of a random string. For example in this string: "localfile/random/1" I'd like to split this string so one variable would be the number on the end of a string, and second variable would be the rest of path. I couldn't google anything.
Any help would be greatly appreciated.
Upvotes: 2
Views: 582
Reputation: 559
How about this?
set s to "blah4"
set l to (get last character of s)
if "1234567890" contains l then log "yes"
Upvotes: 0
Reputation: 11238
This will let you split the string without having to specify the last character before the number string:
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
Upvotes: 1
Reputation: 27633
If you can just split the strings around the last slash:
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}
Both of these only work if the strings always end with numbers:
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
Upvotes: 1