{"id":8346,"date":"2012-09-24T19:49:38","date_gmt":"2012-09-24T14:19:38","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=8346"},"modified":"2012-09-24T23:09:46","modified_gmt":"2012-09-24T17:39:46","slug":"parsing-tweet-for-hashtags-usernames-and-urls-in-java","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/parsing-tweet-for-hashtags-usernames-and-urls-in-java\/","title":{"rendered":"Parsing tweet for Hashtags, Usernames and URLs in Java"},"content":{"rendered":"<p>Hi,<\/p>\n<p>As i am working on Twitter integration in my current project, i needed to display the searched tweets from twitter API, on my view layer. When we query the Twitter API, it returns tweets text in the form of simple string which contains HashTags, Twitter usernames and links to external resources. <\/p>\n<pre>\r\n\r\n<\/pre>\n<p>While displaying them on the UI layer, i wanted to have the proper links for every element like:-<\/p>\n<p><strong>#. Hashtags<\/strong> should be linked to search of tweets containing similar tags on twitter.<br \/>\n<strong>#. Usernames<\/strong> should link to the profile of that user on twitter.<br \/>\n<strong>#. External URLs<\/strong> should be connected to the resource it is pointing to.<\/p>\n<pre>\r\n\r\n<\/pre>\n<p>I searched a lot and found various ways which are really useful if you want to parse the tweet on the server side and thought it worth sharing.<br \/>\nBelow is the method which i used to parse tweet and returns the final html which i can use directly on my UI.<\/p>\n<p>[java]<\/p>\n<p>   String parse(String tweetText) {<\/p>\n<p>        \/\/ Search for URLs<br \/>\n        if (tweetText &amp;&amp; tweetText?.contains(&#8216;http:&#8217;)) {<br \/>\n            int indexOfHttp = tweetText.indexOf(&#8216;http:&#8217;)<br \/>\n            int endPoint = (tweetText.indexOf(&#8216; &#8216;, indexOfHttp) != -1) ? tweetText.indexOf(&#8216; &#8216;, indexOfHttp) : tweetText.length()<br \/>\n            String url = tweetText.substring(indexOfHttp, endPoint)<br \/>\n            String targetUrlHtml=  &quot;&lt;a href=&#8217;${url}&#8217; target=&#8217;_blank&#8217;&gt;${url}&lt;\/a&gt;&quot;<br \/>\n            tweetText = tweetText.replace(url,targetUrlHtml )<br \/>\n        }<\/p>\n<p>        String patternStr = &quot;(?:\\\\s|\\\\A)[##]+([A-Za-z0-9-_]+)&quot;<br \/>\n        Pattern pattern = Pattern.compile(patternStr)<br \/>\n        Matcher matcher = pattern.matcher(tweetText)<br \/>\n        String result = &quot;&quot;;<\/p>\n<p>        \/\/ Search for Hashtags<br \/>\n        while (matcher.find()) {<br \/>\n            result = matcher.group();<br \/>\n            result = result.replace(&quot; &quot;, &quot;&quot;);<br \/>\n            String search = result.replace(&quot;#&quot;, &quot;&quot;);<br \/>\n            String searchHTML=&quot;&lt;a href=&#8217;http:\/\/search.twitter.com\/search?q=&quot; + search + &quot;&#8217;&gt;&quot; + result + &quot;&lt;\/a&gt;&quot;<br \/>\n            tweetText = tweetText.replace(result,searchHTML);<br \/>\n        }<\/p>\n<p>        \/\/ Search for Users<br \/>\n        patternStr = &quot;(?:\\\\s|\\\\A)[@]+([A-Za-z0-9-_]+)&quot;;<br \/>\n        pattern = Pattern.compile(patternStr);<br \/>\n        matcher = pattern.matcher(tweetText);<br \/>\n        while (matcher.find()) {<br \/>\n            result = matcher.group();<br \/>\n            result = result.replace(&quot; &quot;, &quot;&quot;);<br \/>\n            String rawName = result.replace(&quot;@&quot;, &quot;&quot;);<br \/>\n            String userHTML=&quot;&lt;a href=&#8217;http:\/\/twitter.com\/${rawName}&#8217;&gt;&quot; + result + &quot;&lt;\/a&gt;&quot;<br \/>\n            tweetText = tweetText.replace(result,userHTML);<br \/>\n        }<br \/>\n        return tweetText;<br \/>\n    }<br \/>\n[\/java]<\/p>\n<p>The above code will return the tweet text wrapped in HTML elements to make it more UI friendly.<\/p>\n<pre>\r\n\r\n<\/pre>\n<p>This worked for me.<br \/>\nHope it helps.<\/p>\n<pre>\r\n\r\n<\/pre>\n<p>Useful Links:-<\/p>\n<p><a href=\"https:\/\/dev.twitter.com\/docs\/tco-url-wrapper\">https:\/\/dev.twitter.com\/docs\/tco-url-wrapper<\/a><br \/>\n<a href=\"http:\/\/stackoverflow.com\/questions\/8451846\/actual-twitter-format-for-hashtags-not-your-regex-not-his-code-the-actual\">http:\/\/stackoverflow.com\/questions\/8451846\/actual-twitter-format-for-hashtags-not-your-regex-not-his-code-the-actual<\/a><br \/>\n<a href=\"http:\/\/www.simonwhatley.co.uk\/parsing-twitter-usernames-hashtags-and-urls-with-javascript\">http:\/\/www.simonwhatley.co.uk\/parsing-twitter-usernames-hashtags-and-urls-with-javascript<\/a><\/p>\n<pre>\r\n\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Hi, As i am working on Twitter integration in my current project, i needed to display the searched tweets from twitter API, on my view layer. When we query the Twitter API, it returns tweets text in the form of simple string which contains HashTags, Twitter usernames and links to external resources. While displaying them [&hellip;]<\/p>\n","protected":false},"author":19,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":7},"categories":[7],"tags":[1050,1048,700,1047,1049,873,1018,1022,1046,1019],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/8346"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/19"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=8346"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/8346\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=8346"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=8346"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=8346"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}