Regex不是我的强大套件,但我目前正在使用这个regex (\/[\d]+)从tictok获取id。
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。任何帮助都将不胜感激。
(?:|shareId=)(\/[\d]+)发布于 2022-09-03 10:23:44
使用您所显示的示例,请尝试以下regex。下面是显示正则表达式的在线演示。
\bhttps?:\/\/(?:m|www)\.tiktok\.com\/.*\b(?:(?:usr|v|embed|user)\/|\?shareId=)(\d+)\b解释:在这里添加了对已使用正则表达式的详细说明。
\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.发布于 2022-09-03 02:26:28
发布于 2022-09-03 10:17:25
您还可以为问题中的tiktok urls设置特定的模式,并对数字使用捕获组:
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演示。
https://stackoverflow.com/questions/73589058
复制相似问题