php t.me 个链接的正则表达式

php regex for t.me links

我需要一个正则表达式 php 代码来检测如下链接:

t.me/joinchat/AAAAAEEYZxAYyxhdag6z6g

我试过了:

$String = preg_replace("/[t.me][a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/", "", $String);

我需要使用 preg_replace 但上面的代码不起作用 :(

我该怎么办?

根据您的 link 结构,此正则表达式可能有效:

t\.me\/[-a-zA-Z0-9.]+(\/\S*)?

演示:https://regex101.com/r/wEvuBu/1

您的正则表达式 [t.me] 允许单个字符,选项为 t.me。您还缺少域后的 /,这也会导致失败(如果 /,您的 alphanumerical/hyphen 字符 class 将允许找到域和目录已添加)。