{"id":2704,"date":"2009-04-06T18:08:34","date_gmt":"2009-04-06T23:08:34","guid":{"rendered":"http:\/\/granades.com\/?p=2704"},"modified":"2009-04-06T18:08:34","modified_gmt":"2009-04-06T23:08:34","slug":"using-regular-expressions-to-match-twitter-users-and-hashtags","status":"publish","type":"post","link":"https:\/\/granades.com\/?p=2704","title":{"rendered":"Using Regular Expressions to Match Twitter Users and Hashtags"},"content":{"rendered":"<p>If you want to find <a href=\"http:\/\/www.twitter.com\/\">Twitter<\/a> usernames and hashtags in tweets and do something with them, like turn them into links when you&#8217;re displaying them on your website, the most compact way of doing so is through regular expressions. However, most of the articles I looked through on the web mess up the regexp.<\/p>\n<p>Usernames start with a &#8220;@&#8221;, while hashtags start with a &#8220;#&#8221;. Since usernames and hashtags will only have letters, numbers, or underscores in them, most all of the examples on the web use a regexp like so:<\/p>\n<p><pre><code>\r\n@([A-Za-z0-9_]+)\r\n<\/code><\/pre>\n<\/p>\n<p>There&#8217;s only one problem: if you have an email address in a tweet, it&#8217;ll match on that. Run that regular expression on &#8220;Email me at spammy@mailinator.com&#8221; and you&#8217;ll match on &#8220;mailinator&#8221; as a username when it&#8217;s not. What you really need to do is make sure that there&#8217;s nothing in front of the &#8220;@&#8221; or &#8220;#&#8221; but whitespace or the beginning of the string.<\/p>\n<p>For completeness, here&#8217;s example code to add links to both usernames and hashtags in a bunch of different languages.<\/p>\n<p><b>Javascript<\/b><\/p>\n<pre><code markup=\"none\">\r\n<script type=\"text\/javascript\">\r\n    String.prototype.linkify_tweet = function() {\r\n   var tweet = this.replace(\/(^|\\s)@(\\w+)\/g, \"$1@<a href=\\\"http:\/\/www.twitter.com\/$2\\\">$2<\/a>\");\r\n   return tweet.replace(\/(^|\\s)#(\\w+)\/g, \"$1#<a href=\\\"http:\/\/search.twitter.com\/search?q=%23$2\\\">$2<\/a>\");\r\n };\r\n<\/script>\r\n<\/code><\/pre>\n<p><b>PHP<\/b><\/p>\n<pre><code markup=\"none\">\r\nfunction linkify_tweet($tweet) {\r\n    $tweet = preg_replace('\/(^|\\s)@(\\w+)\/',\r\n        '\\1@<a href=\"http:\/\/www.twitter.com\/\\2\">\\2<\/a>',\r\n        $tweet);\r\n    return preg_replace('\/(^|\\s)#(\\w+)\/',\r\n        '\\1#<a href=\"http:\/\/search.twitter.com\/search?q=%23\\2\">\\2<\/a>',\r\n        $tweet);\r\n}\r\n<\/code><\/pre>\n<p><b>Python<\/b><\/p>\n<pre><code markup=\"none\">\r\nimport re\r\n\r\ndef linkify_tweet(tweet):\r\n    tweet = re.sub(r'(\\A|\\s)@(\\w+)', r'\\1@<a href=\"http:\/\/www.twitter.com\/\\2\">\\2<\/a>', tweet)\r\n    return re.sub(r'(\\A|\\s)#(\\w+)', r'\\1#<a href=\"http:\/\/search.twitter.com\/search?q=%23\\2\">\\2<\/a>', tweet)\r\n<\/code><\/pre>\n<p><b>Perl<\/b><\/p>\n<pre><code markup=\"none\">\r\n$s =~ s{(\\A|\\s)@(\\w+)}{$1@<a href=\"http:\/\/www.twitter.com\/$2\">$2<\/a>};\r\n$s =~ s{(\\A|\\s)#(\\w+)}{$1#<a href=\"http:\/\/search.twitter.com\/search?q=%23$2\">$2<\/a>};\r\n<\/code><\/pre>\n<p>(Javascript approach taken from <a href=\"http:\/\/www.simonwhatley.co.uk\/parsing-twitter-usernames-hashtags-and-urls-with-javascript\">Simon Whatley<\/a>)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you want to find Twitter usernames and hashtags in tweets and do something with them, like turn them into links when you&#8217;re displaying them on your website, the most compact way of doing so is through regular expressions. However, most of the articles I looked through on the web mess up the regexp. Usernames &hellip; <a href=\"https:\/\/granades.com\/?p=2704\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Using Regular Expressions to Match Twitter Users and Hashtags<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"footnotes":""},"categories":[],"tags":[],"class_list":["post-2704","post","type-post","status-publish","format-standard","hentry"],"aioseo_notices":[],"aioseo_head":"\n\t\t<!-- All in One SEO 5.0.2 - aioseo.com -->\n\t<meta name=\"description\" content=\"If you want to find Twitter usernames and hashtags in tweets and do something with them, like turn them into links when you&#039;re displaying them on your website, the most compact way of doing so is through regular expressions. However, most of the articles I looked through on the web mess up the regexp. Usernames\" \/>\n\t<meta name=\"robots\" content=\"max-image-preview:large\" \/>\n\t<link rel=\"canonical\" href=\"https:\/\/granades.com\/?p=2704\" \/>\n\t<meta name=\"generator\" content=\"All in One SEO (AIOSEO) 5.0.2\" \/>\n\t\t<meta property=\"og:locale\" content=\"en_US\" \/>\n\t\t<meta property=\"og:site_name\" content=\"Live Granades - Just another WordPress site\" \/>\n\t\t<meta property=\"og:type\" content=\"article\" \/>\n\t\t<meta property=\"og:title\" content=\"Using Regular Expressions to Match Twitter Users and Hashtags - Live Granades\" \/>\n\t\t<meta property=\"og:description\" content=\"If you want to find Twitter usernames and hashtags in tweets and do something with them, like turn them into links when you&#039;re displaying them on your website, the most compact way of doing so is through regular expressions. However, most of the articles I looked through on the web mess up the regexp. Usernames\" \/>\n\t\t<meta property=\"og:url\" content=\"https:\/\/granades.com\/?p=2704\" \/>\n\t\t<meta property=\"article:published_time\" content=\"2009-04-06T23:08:34+00:00\" \/>\n\t\t<meta property=\"article:modified_time\" content=\"2009-04-06T23:08:34+00:00\" \/>\n\t\t<meta name=\"twitter:card\" content=\"summary\" \/>\n\t\t<meta name=\"twitter:title\" content=\"Using Regular Expressions to Match Twitter Users and Hashtags - Live Granades\" \/>\n\t\t<meta name=\"twitter:description\" content=\"If you want to find Twitter usernames and hashtags in tweets and do something with them, like turn them into links when you&#039;re displaying them on your website, the most compact way of doing so is through regular expressions. However, most of the articles I looked through on the web mess up the regexp. Usernames\" \/>\n\t\t<script type=\"application\/ld+json\" class=\"aioseo-schema\">\n\t\t\t{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"BlogPosting\",\"@id\":\"https:\\\/\\\/granades.com\\\/?p=2704#blogposting\",\"name\":\"Using Regular Expressions to Match Twitter Users and Hashtags - Live Granades\",\"headline\":\"Using Regular Expressions to Match Twitter Users and Hashtags\",\"author\":{\"@id\":\"https:\\\/\\\/granades.com\\\/?author=3#author\"},\"publisher\":{\"@id\":\"https:\\\/\\\/granades.com\\\/#organization\"},\"datePublished\":\"2009-04-06T18:08:34+00:00\",\"dateModified\":\"2009-04-06T18:08:34+00:00\",\"inLanguage\":\"en-US\",\"commentCount\":20,\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/granades.com\\\/?p=2704#webpage\"},\"isPartOf\":{\"@id\":\"https:\\\/\\\/granades.com\\\/?p=2704#webpage\"}},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/granades.com\\\/?p=2704#breadcrumblist\",\"itemListElement\":[{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/granades.com\\\/#listItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/granades.com\\\/\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/granades.com\\\/?p=2704#listItem\",\"name\":\"Using Regular Expressions to Match Twitter Users and Hashtags\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/granades.com\\\/?p=2704#listItem\",\"position\":2,\"name\":\"Using Regular Expressions to Match Twitter Users and Hashtags\",\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/granades.com\\\/#listItem\",\"name\":\"Home\"}}]},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/granades.com\\\/#organization\",\"name\":\"Live Granades\",\"description\":\"Just another WordPress site\",\"url\":\"https:\\\/\\\/granades.com\\\/\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/granades.com\\\/?author=3#author\",\"url\":\"https:\\\/\\\/granades.com\\\/?author=3\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/granades.com\\\/?p=2704#webpage\",\"url\":\"https:\\\/\\\/granades.com\\\/?p=2704\",\"name\":\"Using Regular Expressions to Match Twitter Users and Hashtags - Live Granades\",\"description\":\"If you want to find Twitter usernames and hashtags in tweets and do something with them, like turn them into links when you're displaying them on your website, the most compact way of doing so is through regular expressions. However, most of the articles I looked through on the web mess up the regexp. Usernames\",\"inLanguage\":\"en-US\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/granades.com\\\/#website\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/granades.com\\\/?p=2704#breadcrumblist\"},\"author\":{\"@id\":\"https:\\\/\\\/granades.com\\\/?author=3#author\"},\"creator\":{\"@id\":\"https:\\\/\\\/granades.com\\\/?author=3#author\"},\"datePublished\":\"2009-04-06T18:08:34+00:00\",\"dateModified\":\"2009-04-06T18:08:34+00:00\"},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/granades.com\\\/#website\",\"url\":\"https:\\\/\\\/granades.com\\\/\",\"name\":\"Live Granades\",\"description\":\"Just another WordPress site\",\"inLanguage\":\"en-US\",\"publisher\":{\"@id\":\"https:\\\/\\\/granades.com\\\/#organization\"}}]}\n\t\t<\/script>\n\t\t<!-- All in One SEO -->\n\n","aioseo_head_json":{"title":"Using Regular Expressions to Match Twitter Users and Hashtags - Live Granades","description":"If you want to find Twitter usernames and hashtags in tweets and do something with them, like turn them into links when you're displaying them on your website, the most compact way of doing so is through regular expressions. However, most of the articles I looked through on the web mess up the regexp. Usernames","canonical_url":"https:\/\/granades.com\/?p=2704","robots":"max-image-preview:large","keywords":"","webmasterTools":{"miscellaneous":""},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"BlogPosting","@id":"https:\/\/granades.com\/?p=2704#blogposting","name":"Using Regular Expressions to Match Twitter Users and Hashtags - Live Granades","headline":"Using Regular Expressions to Match Twitter Users and Hashtags","author":{"@id":"https:\/\/granades.com\/?author=3#author"},"publisher":{"@id":"https:\/\/granades.com\/#organization"},"datePublished":"2009-04-06T18:08:34+00:00","dateModified":"2009-04-06T18:08:34+00:00","inLanguage":"en-US","commentCount":20,"mainEntityOfPage":{"@id":"https:\/\/granades.com\/?p=2704#webpage"},"isPartOf":{"@id":"https:\/\/granades.com\/?p=2704#webpage"}},{"@type":"BreadcrumbList","@id":"https:\/\/granades.com\/?p=2704#breadcrumblist","itemListElement":[{"@type":"ListItem","@id":"https:\/\/granades.com\/#listItem","position":1,"name":"Home","item":"https:\/\/granades.com\/","nextItem":{"@type":"ListItem","@id":"https:\/\/granades.com\/?p=2704#listItem","name":"Using Regular Expressions to Match Twitter Users and Hashtags"}},{"@type":"ListItem","@id":"https:\/\/granades.com\/?p=2704#listItem","position":2,"name":"Using Regular Expressions to Match Twitter Users and Hashtags","previousItem":{"@type":"ListItem","@id":"https:\/\/granades.com\/#listItem","name":"Home"}}]},{"@type":"Organization","@id":"https:\/\/granades.com\/#organization","name":"Live Granades","description":"Just another WordPress site","url":"https:\/\/granades.com\/"},{"@type":"Person","@id":"https:\/\/granades.com\/?author=3#author","url":"https:\/\/granades.com\/?author=3"},{"@type":"WebPage","@id":"https:\/\/granades.com\/?p=2704#webpage","url":"https:\/\/granades.com\/?p=2704","name":"Using Regular Expressions to Match Twitter Users and Hashtags - Live Granades","description":"If you want to find Twitter usernames and hashtags in tweets and do something with them, like turn them into links when you're displaying them on your website, the most compact way of doing so is through regular expressions. However, most of the articles I looked through on the web mess up the regexp. Usernames","inLanguage":"en-US","isPartOf":{"@id":"https:\/\/granades.com\/#website"},"breadcrumb":{"@id":"https:\/\/granades.com\/?p=2704#breadcrumblist"},"author":{"@id":"https:\/\/granades.com\/?author=3#author"},"creator":{"@id":"https:\/\/granades.com\/?author=3#author"},"datePublished":"2009-04-06T18:08:34+00:00","dateModified":"2009-04-06T18:08:34+00:00"},{"@type":"WebSite","@id":"https:\/\/granades.com\/#website","url":"https:\/\/granades.com\/","name":"Live Granades","description":"Just another WordPress site","inLanguage":"en-US","publisher":{"@id":"https:\/\/granades.com\/#organization"}}]},"og:locale":"en_US","og:site_name":"Live Granades - Just another WordPress site","og:type":"article","og:title":"Using Regular Expressions to Match Twitter Users and Hashtags - Live Granades","og:description":"If you want to find Twitter usernames and hashtags in tweets and do something with them, like turn them into links when you're displaying them on your website, the most compact way of doing so is through regular expressions. However, most of the articles I looked through on the web mess up the regexp. Usernames","og:url":"https:\/\/granades.com\/?p=2704","article:published_time":"2009-04-06T23:08:34+00:00","article:modified_time":"2009-04-06T23:08:34+00:00","twitter:card":"summary","twitter:title":"Using Regular Expressions to Match Twitter Users and Hashtags - Live Granades","twitter:description":"If you want to find Twitter usernames and hashtags in tweets and do something with them, like turn them into links when you're displaying them on your website, the most compact way of doing so is through regular expressions. However, most of the articles I looked through on the web mess up the regexp. Usernames"},"aioseo_meta_data":{"post_id":"2704","title":null,"description":null,"keywords":null,"keyphrases":null,"primary_term":null,"canonical_url":null,"og_title":null,"og_description":null,"og_object_type":"default","og_image_type":"default","og_image_url":null,"og_image_width":null,"og_image_height":null,"og_image_custom_url":null,"og_image_custom_fields":null,"og_video":null,"og_custom_url":null,"og_article_section":null,"og_article_tags":null,"twitter_use_og":false,"twitter_card":"default","twitter_image_type":"default","twitter_image_url":null,"twitter_image_custom_url":null,"twitter_image_custom_fields":null,"twitter_title":null,"twitter_description":null,"schema":{"blockGraphs":[],"customGraphs":[],"default":{"data":{"Article":[],"Course":[],"Dataset":[],"FAQPage":[],"Movie":[],"Person":[],"Product":[],"ProductReview":[],"Car":[],"Recipe":[],"Service":[],"SoftwareApplication":[],"WebPage":[]},"graphName":"","isEnabled":true},"graphs":[]},"schema_type":"default","schema_type_options":null,"pillar_content":false,"robots_default":true,"robots_noindex":false,"robots_noarchive":false,"robots_nosnippet":false,"robots_nofollow":false,"robots_noimageindex":false,"robots_noodp":false,"robots_notranslate":false,"robots_max_snippet":null,"robots_max_videopreview":null,"robots_max_imagepreview":"large","priority":null,"frequency":null,"local_seo":null,"breadcrumb_settings":null,"limit_modified_date":false,"ai":null,"created":"2022-06-26 00:31:43","updated":"2025-11-03 06:23:18","focus_keyword":null,"additional_keywords":null,"truseo_locale":null,"seo_analyzer_scan_date":null},"aioseo_breadcrumb":"<div class=\"aioseo-breadcrumbs\"><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/granades.com\/\" title=\"Home\">Home<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">\u00bb<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\tUsing Regular Expressions to Match Twitter Users and Hashtags\n\t\t<\/span><\/div>","aioseo_breadcrumb_json":[{"label":"Home","link":"https:\/\/granades.com\/"},{"label":"Using Regular Expressions to Match Twitter Users and Hashtags","link":"https:\/\/granades.com\/?p=2704"}],"_links":{"self":[{"href":"https:\/\/granades.com\/index.php?rest_route=\/wp\/v2\/posts\/2704","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/granades.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/granades.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/granades.com\/index.php?rest_route=\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/granades.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2704"}],"version-history":[{"count":11,"href":"https:\/\/granades.com\/index.php?rest_route=\/wp\/v2\/posts\/2704\/revisions"}],"predecessor-version":[{"id":2715,"href":"https:\/\/granades.com\/index.php?rest_route=\/wp\/v2\/posts\/2704\/revisions\/2715"}],"wp:attachment":[{"href":"https:\/\/granades.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2704"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/granades.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2704"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/granades.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2704"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}