How do I extract 15855 from
http://www.macupdate.com/info.php/id/15855/praat
Thank you
Hi,
something like this
set theURL to "http://www.macupdate.com/info.php/id/15855/praat"
set {TID, text item delimiters} to {text item delimiters, "/"}
set theNumber to text item -2 of theURL
set text item delimiters to TID
display dialog theNumber
Thanks Stefan
It works.
The only thing is that I cannot assume that the number is the 2nd to last item.
I really need to check every item to see if they are the number. Sometimes the number is the last.
One thing for sure is that there is only one number in the URL address
Try something like this:
set test to "http://www.macupdate.com/info.php/id/15855/praat"
do shell script "echo " & quoted form of test & ¬
" | /usr/bin/grep --only-matching '[0-9]\\+'"
set example to result
Thanks Bruce
We still have a little problem
set test to “http://www.macupdate.com/info.php/id/a1”
the result is 1.
If there are any letters with the number it is not acceptable it must be an integer
Also if there is no number in the URL
“http://www.macupdate.com/info.php/id/a”
it fails with error the command exited with a non-zero status.
I suppose we need to check each item in the URL and accept the only integer in it to be safe.
something like
repeat with i = 1 to number of items in theUrl
if item i is an integer then
This works for me.
Problem solved, thanks to all that helped.
Any more elegant or shorter ways to do it anyone?
set theURL to "http://www.macupdate.com/info.php/id/b/15855/myAppl"
set {TID, text item delimiters} to {text item delimiters, "/"}
set itemsNo to count text items in theURL
set thisItem to 0
repeat with i from 1 to itemsNo
try
set thisItem to (text item i of theURL) as number
end try
if thisItem > 0 then exit repeat
end repeat
set text item delimiters to TID
return thisItem & i
returns {15855, 6}
set theURL to “http://www.macupdate.com/info.php/id/b123444/myAppl”
returns {0, 7}
Alternatively:
set test to "http://www.macupdate.com/info.php/id/15855/praat"
do shell script "/usr/bin/ruby -e 'require \"uri\"; uri = URI.parse(ARGV[0]);" & ¬
" match = uri.path.chomp(\"/\").concat(\"/\").match(/\\/[0-9]+\\//);" & ¬
" puts (match ? match[0][1..-2] : \"\")' " & quoted form of test
set example to result
Thanks Bruce
Sorry for the late reply