Advertisement
Guest User

Untitled

a guest
Jan 21st, 2018
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.30 KB | None | 0 0
  1. - (NSArray *)referenceLinksInString:(NSString *)contentString{
  2. NSString *wildString = @"*[*]:*http*"; //This is where you define your match string.
  3. NSPredicate *matchPred = [NSPredicate predicateWithFormat:@"SELF LIKE[cd] %@", wildString];
  4. /*
  5. Breaking it down:
  6. SELF is the string you're testing
  7. [cd] makes the test case-insensitive
  8. LIKE is one of the predicate search possiblities. It's NOT regex, but lets you use wildcards '?' for one character and '*' for any number of characters
  9. MATCH (not used) is what you would use for Regex. And you'd set it up similiar to LIKE. I don't really know regex, and I can't quite get it to work. But that might be because I don't know regex.
  10. %@ you need to pass in the search string like this, rather than just embedding it in the format string. so DON'T USE something like [NSPredicate predicateWithFormat:@"SELF LIKE[cd] *[*]:*http*"]
  11. */
  12.  
  13. NSMutableArray *referenceLinks=[NSMutableArray new];
  14.  
  15. //enumerateLinesUsing block seems like a good way to go line by line thru the note and test each line for the regex match of a reference link. Downside is that it uses blocks so requires 10.6+. Let's get it to work and then we can figure out a Leopard friendly way of doing this; which I don't think will be a problem (famous last words).
  16. [contentString enumerateLinesUsingBlock:^(NSString *line, BOOL *stop) {
  17. if([matchPred evaluateWithObject:line]){
  18. // NSLog(@"%@ matched",line);
  19. NSString *theRef=line;
  20. //theRef=[line substring...] here you want to parse out and get just the name of the reference link we'd want to offer up to the user in the autocomplete
  21. //and maybe trim out whitespace
  22. theRef = [theRef stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
  23. //check to make sure its not empty
  24. if(![theRef isEqualToString:@""]){
  25. [referenceLinks addObject:theRef];
  26. }
  27. }
  28. }];
  29. //create an immutable array safe for returning
  30. NSArray *returnArray=[NSArray array];
  31. //see if we found anything
  32. if(referenceLinks&&([referenceLinks count]>0))
  33. {
  34. returnArray=[NSArray arrayWithArray:referenceLinks];
  35. }
  36. [referenceLinks release];
  37. return returnArray;
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement