質問

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 handle.

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>"
)
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top