首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从Url Regex问题获取tictok id

从Url Regex问题获取tictok id
EN

Stack Overflow用户
提问于 2022-09-03 01:17:19
回答 5查看 266关注 0票数 1

Regex不是我的强大套件,但我目前正在使用这个regex (\/[\d]+)从tictok获取id。

代码语言:javascript
复制
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

我从所有链接中获得id,除了带有shareId=xxxxxxxxxxx的链接。我将原来的regex修改如下,但仍然没有从shareId=xxxxxxxxxxx链接中获得id。任何帮助都将不胜感激。

代码语言:javascript
复制
(?:|shareId=)(\/[\d]+)
EN

回答 5

Stack Overflow用户

发布于 2022-09-03 10:23:44

使用您所显示的示例,请尝试以下regex。下面是显示正则表达式的在线演示

代码语言:javascript
复制
\bhttps?:\/\/(?:m|www)\.tiktok\.com\/.*\b(?:(?:usr|v|embed|user)\/|\?shareId=)(\d+)\b

解释:在这里添加了对已使用正则表达式的详细说明。

代码语言:javascript
复制
\bhttps?:\/\/         ##Mentioning word boundary to avoid partial matches for http/https followed by : 2 slashes.
(?:m|www)             ##In a non-capturing group matching m OR www
\.tiktok\.com\/       ##Followed by .tiktok.com
.*\b                  ##Doing greedy match followed by word boundary.
(?:                   ##Starting one non-capturing group here.
  (?:                 ##Starting one more non-capturing group here.
    usr|v|embed|user  ##matching usr OR v OR embed OR user here.
  )                   ##Closing previously opened non-capturing group here.
  \/                  ##Followed by a literal / here.
  |                   ##putting OR condition here.
  \?shareId=          ##matching ?ShareId= here.
)                     ##Closing firstly opened non-capturing group here.
(\d+)                 ##Creating one and only capturing group which has digits in it.
\b                    ##Followed by word boundary here to avoid partial matches.
票数 2
EN

Stack Overflow用户

发布于 2022-09-03 02:26:28

已经给出了一些可行的答案,但我想提出一个漂亮的解决方案:

代码语言:javascript
复制
([=/][\d]+)

第一组检查“=”或“/”符号。请参阅https://regex101.com/r/9uNJZ1/1

票数 1
EN

Stack Overflow用户

发布于 2022-09-03 10:17:25

您还可以为问题中的tiktok urls设置特定的模式,并对数字使用捕获组:

代码语言:javascript
复制
https?://(?:m|www)\.tiktok\.com\b\S*?(?:/(?:use?r|v|embed)/|\bshareId=)(\d+)\b

解释

  • https?://(?:m|www)\.tiktok\.com\b匹配urls的开头
  • \S*?匹配可选的非空格字符,尽可能少
  • (?:/(?:use?r|v|embed)/|\bshareId=)匹配/usr/ /user/ /v /embed/ shareId=中的一个
  • (\d+)捕获组1,匹配1+数字
  • \b阻止部分单词匹配的单词边界

看一个regex演示

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73589058

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档