开发者

Parsing Tweets for #hashtags and @usernames using iOS for multiple languages

I want to transform a tweet for display in a UIWebView. I want to convert the #hashtags and @usernames into html links which I then will ha开发者_JAVA技巧ndle.

I have found some solutions using regular expressions but most seem to have problems as email addresses and web links can have the '@' character in them.

Does anyone know of an appropriate way to do this in Objective-C? Can it be done using regular expressions, which are available in iOS 4 and later? Or do I need to get funky with a parser?

Oh yeah, forgot to mention, I want this to work for all languages. (arabic, chinese, german etc...)


If you are doing this in a webview, why don't you go ahead and use javascript? I've done this in a project, where links had to be found automatically. I couldn't depend on the data detector of the webview so a short javascript helped:

someTweet.replace(
   /(\b(https?):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig,
   "<a href='$1'>$1</a>"
)

The regex is simple and might not catch every url out there but it did most of the time. Transferring this to usernames and hashtags shouldn't be too difficult.

usernames consist of characters, numbers and _ so @([1-9a-zA-Z_]+) will match them.

someTweet.replace(
   /@([1-9a-zA-Z_]+)/,
   "<a href='http://twitter.com/$1'>@$1</a>")
)

hashtags, I am not sure about these: I assume they are anything starting with a # and terminates with a whitespace, # or @, but that's just guessing. #([^\s#@]*) might work.

someTweet.replace(
   /#([^\s#@]*)/,
   "<a href='http://twitter.com/search?q=%23$1'>#$1</a>"
)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜