0

My problem is finding the position of a specific word (ex: span) in an NSString containing html text, and replace this word.

For example, if my target word is span, my replacement text should look like:

<SPAN style="BACKGROUND-COLOR: #FF0000">span</SPAN>

If my starting text is:

<span class='i_04_12_000000'>this is a span</span>

My desired result would be:

<span class='i_04_12_000000'>this is a <SPAN style="BACKGROUND-COLOR: #FF0000">span</SPAN></span>

Obviously if I just search for span I won't find just the occurrence inside the html tag. Ideas on how to locate the correct string, even with a NSRange ?

UPDATE:

This is the final solution: ;)

    +(NSString *)emphasizeString:(NSString *)stringToEmphasize inText:(NSString *)fullText
{
    NSString * regExp = [NSString stringWithFormat:@"(?i)([\\s\\.,>'-])(%@)([\\s\\.,;!\\?\\)<])",stringToEmphasize];;
    NSArray * regexResult = [fullText arrayOfCaptureComponentsMatchedByRegex:regExp];
    NSString * result = [NSString stringWithString:fullText];
    if([regexResult count] >0)
    {
        for (NSArray * match in regexResult)
        {

            NSString * all = [match objectAtIndex:0];
            NSString * before = [match objectAtIndex:1];
            NSString * matched = [match objectAtIndex:2];
            NSString * after = [match objectAtIndex:3];

            result = [result stringByReplacingOccurrencesOfString:all 
                                                       withString:[NSString stringWithFormat:@"%@<SPAN style=\"BACKGROUND-COLOR: #FF0000\">%@</SPAN>%@",before, matched, after]
                                                          options:NSCaseInsensitiveSearch 
                                                            range: [result rangeOfString:all]];
        }
    }
    NSLog(@"%@",result);
    return result;
}
4
  • example html string: <span class='i_04_12_000000'>this is a span</span> result: <span class='i_04_12_000000'>this is a <SPAN style=\"BACKGROUND-COLOR: #FF0000\">span</SPAN> </span> Commented Dec 7, 2011 at 17:21
  • are these specific words you're looking for surrounded by any kind of markup, e.g: <span>? Commented Dec 7, 2011 at 17:25
  • 1
    Better read this stackoverflow.com/questions/1732348/… Commented Dec 7, 2011 at 17:35
  • @Kappe you should post your solution as an answer to your question. After two days, you can accept it. Commented Dec 28, 2011 at 20:04

2 Answers 2

1

This is the final solution: ;)

    +(NSString *)emphasizeString:(NSString *)stringToEmphasize inText:(NSString *)fullText
{
    NSString * regExp = [NSString stringWithFormat:@"(?i)([\\s\\.,>'-])(%@)([\\s\\.,;!\\?\\)<])",stringToEmphasize];;
    NSArray * regexResult = [fullText arrayOfCaptureComponentsMatchedByRegex:regExp];
    NSString * result = [NSString stringWithString:fullText];
    if([regexResult count] >0)
    {
        for (NSArray * match in regexResult)
        {

            NSString * all = [match objectAtIndex:0];
            NSString * before = [match objectAtIndex:1];
            NSString * matched = [match objectAtIndex:2];
            NSString * after = [match objectAtIndex:3];

            result = [result stringByReplacingOccurrencesOfString:all 
                                                       withString:[NSString stringWithFormat:@"%@<SPAN style=\"BACKGROUND-COLOR: #FF0000\">%@</SPAN>%@",before, matched, after]
                                                          options:NSCaseInsensitiveSearch 
                                                            range: [result rangeOfString:all]];
        }
    }
    NSLog(@"%@",result);
    return result;
}
Sign up to request clarification or add additional context in comments.

Comments

0

Try the following regex: /<[a-z]+.*?>/

EDIT: Update as per your new instructions:

Since the html tag preceding the text you want to capture has a variable number of characters, you can't use lookbehind to grab only the target word preceded by the opening tag. However, you could use:

/(<)([a-z]+)(.*?>.*?)(\2)(.*?</\1>)/

to capture an entire tag where the tag's name is repeated somewhere in its body text and isolate that piece of text in a capture group. Then, use a replacement method to with that regex as the search pattern, and "$1$2$3<span style=\"background-color: #FF0000;\">$4</span>$5" (where $4 indicates the fourth capture group containing our target text) for your replacement string.

3 Comments

This pattern will only locate <span class='i_04_12_000000'> and not the span between the tags.
@arcain Check my edited post to see if it's more along the lines of what you're looking for.
Hi Aaron, this regex don't seems work, i update the answer with my solution

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.