views:

126

answers:

5

Lets assume I have the string:

"Hello I like your shoes #today...!"

I am tokenizing the string by spaces:

return [string componentsSeparatedByString:@" "];

So my array contains:

Hello
I
like
your
shoes
#today...!

I want to focus on "#today...!" any word that has a # in the prefix I am changing the font color.

How can I make sure that only "#today" has its font color changed?

I would basically like to figure out if a word has a punctuation mark at the end, and change the color for characters before the punctuation mark.

+2  A: 

Consider using RegexKitLite and this method:

- (NSRange)rangeOfRegex:(NSString *)regex
                options:(RKLRegexOptions)options
                inRange:(NSRange)range
                capture:(NSInteger)capture
                  error:(NSError **)error;

The regex you'd want is something like @"#\\S+\\b". This regex says "Find a '#' character followed by 1 or more non-space characters followed by a word boundary".

This would then return the range of the matched regex inside the string to match.

nall
A regex of `#\w+\b` (or `@"#\\w+\\b"`, remember to double those backslashes in a string literal) might be a better choice. `\w+` would match "word characters", whereas `\S+` matches "non white-space characters". The regex `(?w)#\w+\b` might be an ever better choice if you have to deal with "non-latin" words and languages- the `(?w)` activates the enhanced word-breaking functionality.
johne
I'll edit and add the escapes -- doh! Great point about the ?w.
nall
+1  A: 

I'm confused by your question. Are you trying to detect string with punctuation marks at the end or with # marks at the beginning?

In any case:

for (NSString *word in [string componentsSeparatedByString:@" "]) {
    if ([word hasPrefix:@"#"])NSLog(@"%@ starts with #",word);
    if ([word hasSuffix:@"!"])NSLog(@"%@ end with !",word);
}
prendio2
I want to be able to handle any punctuation mark in the suffix. So essentially in this example, I would want to cut off the ...! and have my word be #today
Sheehan Alam
Another example could be "#today,hello!" I want to be able to parse only #today
Sheehan Alam
+1  A: 

You could do something like:

if ([[NSCharacterSet symbolCharacterSet] characterIsMember:[word characterAtIndex:0]]) NSLog(@"%@", word);

This is to test for a symbol at the beginning of the string--to test at the end, you would use [word characterAtIndex:([word length] - 1)]

EDIT: OK, I think I understand the question now. If you want want to only change the color of characters before the color set, you could do something along the lines of:

NSRange punctCharRange = [word rangeOfCharacterFromSet:[NSCharacterSet punctuationCharacterSet]];
for (int i = 0; i < punctCharRange.location; i++) {
    //change the color of the character
}
eman
I think your `punctCharRange.location` will probably be `0` for any `word` starting with `#`, which is why I used `options:NSBackwardsSearch` with `alphanumericCharacterSet`.
Isaac
`#` isn't in `punctuationCharacterSet`, so I think it should be fine--both implementations should work (although testing against `alphanumericCharacterSet` will also pick up non-punctuation symbols, which may or may not be a good thing).
eman
+1. Ahh, okay, so depending on what characters are to be removed from the tail end of the string, both your method and mine might need to be altered to use a different/custom character set.
Isaac
+1  A: 

The following code should look from the end of word toward the beginning until it finds a character that is not alphanumeric, test if it found such a character and if so, remove anything after that character. (untested code)

NSString *wordWithoutTrailingNonAlphanum;
NSRange firstNonAlphanumCharFromEnd =
        [word rangeOfCharacterFromSet:[NSCharacterSet alphanumericCharacterSet]
                              options:NSBackwardsSearch];
if (firstNonAlphanumCharFromEnd.location != NSNotFound) {
    wordWithoutTrailingNonAlphanum =
            [word substringToIndex:(firstNonAlphanumCharFromEnd.location + 1)];
}
Isaac
A: 

I tried tokenizing all words that begin with # and splitting it based on the NSPunctuationCharacterSet. This works, however I lose the punctuation marks.

Sheehan Alam