【发布时间】:2022-12-29 10:47:14
【问题描述】:
我试图用一个正则表达式匹配所有这些:
https://m.tiktok.com/h5/share/usr/6641141594707361797.htmlhttps://m.tiktok.com/v/6749869095467945218.htmlhttps://www.tiktok.com/embed/6567659045795758085https://www.tiktok.com/share/user/6567659045795758085https://www.tiktok.com/trending?shareId=6744531482393545985https://www.tiktok.com/@burntpizza89/video/7067695578729221378?is_copy_url=1&is_from_webapp=v1https://www.tiktok.com/@burntpizza89/video/is_copy_url=1&is_from_webapp=v1&item_id=7067695578729221378-
https://vm.tiktok.com/ZMF6rgvXY/
除了最后一个,它工作正常。当前的正则表达式是:
"\bhttps?:\/\/(?:m|www|vm)\.tiktok\.com\/.*\b(?:(?:usr|v|embed|user|video)\/|\?shareId=|\&item_id=)(\d+)\b"gm
它完美地处理了所有这些数字 ID(.tiktok.com/@burntpizza89/video/7067695578729221378),但我还需要以某种方式匹配这些类型的链接,其中包含一些特定的 url (.tiktok.com/ZMF6rgvXY/) with just one regex. So for the match I would get or digit-only id, or the url which contains digits and characters.
【问题讨论】:
-
最后一个 ID 的特征是什么?假设至少有 7 个单词字符,必须包含一个数字或一个大写字母......你可以在你的模式中添加第二个捕获组,就像这个演示:
\bhttps?:\/\/(?:m|www|vm)\.tiktok\.com\/\S*?\b(?:(?:(?:usr|v|embed|user|video)\/|\?shareId=|\&item_id=)(\d+)|(?=\w{7})(\w*?[A-Z\d]\w*)(?=\s|\/$))\b