且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在 Twitter 中提取提及的正则表达式

更新时间:2023-02-17 22:57:06

像这样在最后一组添加下划线:

Add an underscore to the last set like this:

(?<=^|(?<=[^a-zA-Z0-9-_\.]))@([A-Za-z]+[A-Za-z0-9_]+)

Regex101 演示

附带说明,Twitter 处理规则允许您使用以数字开头的用户名 &下划线也是如此.因此,要提取 twitter 句柄,正则表达式可能很简单:@\w{1,15} (允许字符、数字和下划线,包括 15 个字符的限制).根据可能使用正则表达式的位置,需要一些额外的前瞻/后视.

On a side note, Twitter Handle rules allow you to have usernames starting with numbers & underscores as well. So to extract twitter handles a regex could be as simple as: @\w{1,15} (allows characters, numbers and underscores and includes the 15 character limit). Will need some additional lookaheads/lookbehinds based on where the regex might be used.