Home > Back-end >  Get tictok id from Url Regex issue
Get tictok id from Url Regex issue

Time:09-03

Regex is not my strong suite but I am currently using this regex (\/[\d] ) to get the id from tictok urls.

 https://m.tiktok.com/h5/share/usr/6641141594707361797.html
 https://m.tiktok.com/v/6749869095467945218.html
 https://www.tiktok.com/embed/6567659045795758085
 https://www.tiktok.com/share/user/6567659045795758085
 https://www.tiktok.com/trending?shareId=6744531482393545985

I get the id from all the links except the one with the shareId=xxxxxxxxxxx. I modified my original regex to the following below and still not getting the id from the link with shareId=xxxxxxxxxxx. Any help will be greatly appreciated.

(?:|shareId=)(\/[\d] )

CodePudding user response:

You can use this regex:

'''(?:\/|shareId=)\d (?!.*\/)'''g

It matches either a slash / or shareId= followed one or more digits.

It then adds an extra check that there are no more slashes on that line - this way slashes in names won't match.

Regex101 link

CodePudding user response:

Okay I used the suggestions given and made the adjustments and got the following regex. see solution and link below. Thanks for all the help.

(?:|shareId=^)\d{19}

https://regex101.com/r/OY8B6h/1

CodePudding user response:

There are working answers given already, but I want to suggest an elegant-looking solution:

([=/][\d] )

The first set checks for either '=' or '/' symbol. See https://regex101.com/r/9uNJZ1/1

  • Related