user3746428 user3746428 - 6 months ago 19
iOS Question

Get app's ID number from iTunes URL string

I need to find a way to parse an app's ID number from an iTunes URL. For example, this URL -

https://itunes.apple.com/gb/app/mouse-times-florida/id1021402097?mt=8

Would give me this - 1021402097

I was considering different ways to do this with string splitting and such, but they all seem kind of hacky. I suppose I could just split the string after 'id' and end the split after 10 digits, but there's a risk that IDs could be increased to 11 digits in the future.

Anyone have a better idea using regex or something?

Answer

If you can make the following assumptions:

  • The id will only contain digits.
  • The number of digits, which currently is 10, will only grow.
  • In the url, the id... part contains the id.

Then, you can use a simple regex such as this: id([\d]{10,}), in which your id will be captured in group 1. Working example: https://regex101.com/r/nD0mZ8/1

Comments