iphone - regex to find hashtags in tweet not working correctly -
i trying build function find hashtags in tweest. , surround them html <a> tag. can link them. here do.
nserror* error = nil; nsregularexpression* regex = [nsregularexpression regularexpressionwithpattern:@"(?:\\s|\\a)[##]+([a-za-z0-9-_]+)" options:0 error:&error]; nsarray* matches = [regex matchesinstring:tweettext options:0 range:nsmakerange(0, [tweettext length])]; ( nstextcheckingresult* match in matches ) { nsstring* matchtext = [tweettext substringwithrange:[match range]]; nsstring *matchtext2 = [matchtext stringbytrimmingcharactersinset:[nscharacterset whitespacecharacterset]]; nsstring *search = [matchtext2 stringbyreplacingoccurrencesofstring:@"#" withstring:@""]; nsstring *searchhtml= [nsstring stringwithformat:@"<a href='https://twitter.com/search?q=%%23%@'>%@</a>",search,matchtext]; tweettext = [tweettext stringbyreplacingoccurrencesofstring:matchtext withstring:searchhtml]; nslog(@"match: %@", tweettext); } before execute function, tweettext looped through function find url. tweet can contain following. <a href='http://google.be' target='_blank'>http://google.be</a>
now places tag around other links , not around hashtags.
can me this.
tip
i trying transform following java code obj-c
string patternstr = "(?:\\s|\\a)[##]+([a-za-z0-9-_]+)" pattern pattern = pattern.compile(patternstr) matcher matcher = pattern.matcher(tweettext) string result = ""; // search hashtags while (matcher.find()) { result = matcher.group(); result = result.replace(" ", ""); string search = result.replace("#", ""); string searchhtml="<a href='http://search.twitter.com/search?q=" + search + "'>" + result + "</a>" tweettext = tweettext.replace(result,searchhtml); } edit
gers, kijken er al naar uit! “@gerspardoel: zitten in belgiĆ« straks naar genk!!<a href='<a href<a href='https://twitter.com/search?q=%23='http'>='http</a>s://twitter.com/search?q=%23https:/'>https:/</a>/twitter.com/search?q=%23engaan'> #engaan</a>” #gos12 #genk #fb
the problem you're modifying tweettext variable (tweettext = ...) you're looping through matches. imagine happens next time code enters loop? substringwithrange not work since created on original string. try rectify problem , if you're unable it, check solution here: http://pastebin.com/dyqqtrza
edit: adding solution here:
nserror* error = nil; nsregularexpression* regex = [nsregularexpression regularexpressionwithpattern:@"(?:\\s|\\a)[##]+([a-za-z0-9-_]+)" options:0 error:&error]; nsarray* matches = [regex matchesinstring:tweettext options:0 range:nsmakerange(0, [tweettext length])]; nsstring* processedstring = [[tweettext copy] autorelease]; ( nstextcheckingresult* match in matches ) { nsstring* matchtext = [tweettext substringwithrange:[match range]]; nsstring *matchtext2 = [matchtext stringbytrimmingcharactersinset:[nscharacterset whitespacecharacterset]]; nsstring *search = [matchtext2 stringbyreplacingoccurrencesofstring:@"#" withstring:@""]; nsstring *searchhtml= [nsstring stringwithformat:@"<a href='https://twitter.com/search?q=%%23%@'>%@</a>",search,matchtext]; processedstring = [processedstring stringbyreplacingoccurrencesofstring:matchtext withstring:searchhtml]; nslog(@"match: %@", processedstring); }
Comments
Post a Comment