title: NSScanner based on last occurrence of substring "@"
body: <p>I have a user name system similar to twitters using the @ symbol. </p>
  
  <p>I have it set up to message users by typing @username. This is basically identical to mentions in Twitter. </p>
  
  <p>So far I have been able to detect the text following the @ symbol and to stop detecting when it hits white space. This has been done perfectly with NSScanner. </p>
  
  <p>My problem is if I want to message multiple @usernames, the NSScanner will only detect the first occurrence. I need it to detect the latest occurrence. I have found this as a useful tool:</p>
  
  <pre><code>NSRange lastSymbol = [text rangeOfString:@"@" options:NSBackwardsSearch];
  </code></pre>
  
  <p>I can't seem to figure out how to properly use this in conjunction with NSScanner. Here are 2 examples of my code attempts.</p>
  
  <p>Example 1:</p>
  
  <pre><code>NSString *user = nil;
  NSString *text = textView.text;
  
  NSRange lastSymbol = [text rangeOfString:@"@" options:NSBackwardsSearch];
  
  if (lastSymbol.location != NSNotFound) {
  
      NSScanner *userScanner = [NSScanner scannerWithString:text];
      [userScanner scanUpToString:@"@" intoString:nil];
  
      NSCharacterSet *charset = [NSCharacterSet characterSetWithCharactersInString:@"@"];
  
      if (![userScanner isAtEnd]) {
  
          [userScanner scanUpToCharactersFromSet:charset intoString:nil];
          [userScanner scanCharactersFromSet:charset intoString:nil]
          [userScanner scanUpToCharactersFromSet:charset intoString:&amp;user];
  
  
      }
  }
  
  
  if (user.length &gt; 0) {
  
      NSRange whiteSpaceRange = [user rangeOfCharacterFromSet:[NSCharacterSet whitespaceCharacterSet]];
      if (whiteSpaceRange.location != NSNotFound) {
  
          // White space, username has ended
  
      } else {
  
          // Still getting username
          NSLog(@"%@", user);
  
      }
  
  }
  </code></pre>
  
  <p>As you can see, I am just not quite sure how to make these work together to produce the results I am looking for. Any ideas? Thanks!</p>
  
  <p>**************************UPDATE****************************</p>
  
  <p>To be more specific, when a user types the @ symbol and starts typing a username, I want to display a list of their friends based on what they type. So naturally, I want to pull the string following the @ symbol and as the user keeps typing, keep updating the list of their friends. Once the user types a space, we assume they are done typing the user name (usernames cannot contain spaces) so I then stop displaying the table view.</p>
  
  <p>The above code so far is simply detecting and pulling the string following the @ symbol. Here is the output right now:</p>
  
  <p>Textfield Text: Hello @username</p>
  
  <p><img src="https://i.stack.imgur.com/61F49.png" alt="enter image description here"></p>
  
  <p>This works perfectly, as you can see I can keep checking this text against an array of friends. </p>
  
  <p>Let's say the user wants to message another user in the same text like so:</p>
  
  <p>Hello @username, this is my friend @goodfriend</p>
  
  <p>This time it still displays the first @username, but ignore the second @goodfriend.</p>
  
  <p>I figure the best way to do this is somehow tell the NSScanner to reference the LAST occurrence of the @ symbol, because what I have now is only referencing the first @ symbol. </p>
  
  <p>This might also have to do with the way I am detecting the white space to know the user is done typing the username, and does not need any more suggestions. </p>
  
  <p>Hope this helps, thanks! </p>