0

Hi im trying to remove som HTML string from a web response. I want to remove <pre><a style="" name="output-line-1">1</a>, were the who instances of number "1"varies, but is always a digit. but how do i write the regex method for removing this? Below is what i have got so far:

-(NSString *)stringByStrippingHTML:(NSString*)str
{
    NSRange r;
   while ((r = [str rangeOfString:@"/^<pre><a style=\"\"name=\"output-line-([0-9])\">([0-9])</a>"  options:NSRegularExpressionSearch]).location != NSNotFound){

        str = [str stringByReplacingCharactersInRange:r withString:@""];

    }
 }

Basically I want to remove a substring with random number in it... In some instances of the substring the 1 is replaced, so that any similar string gets acknowledged, for example it could be output-line-999. How do i combine the range of string so i can both describe the string and specify to find any similar string with any number?

I want to remove both the HTML and the numbers.

1
  • If it's an issue with compilation, it'll be because you haven't escaped the quotation marks in the html string. Replace " with \" (apart from the outer ones) Commented May 31, 2013 at 12:38

2 Answers 2

1

This regular expression should work:

[str rangeOfString:@"<pre><a style=\"\" name=\"output-line-[0-9]+\">[0-9]+</a>"  options:NSRegularExpressionSearch];

I thnk the problem is that there ins't a space before name in your reg expression

Sign up to request clarification or add additional context in comments.

5 Comments

It was good, i updated the questioned from what i learned, but it dont really work yet ;)
I just found the problem, use the regular expression I just put
@DavidKarlsson This regex is missing a + after the [0-9] references, which should be [0-9]+.
thanks, added also to clear the blank lines left : str = [str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
What does the + signs do? are numbers higher than 9 supported by this regex?
0

Using your original while loop, you can:

-(NSString *)stringByStrippingHTML:(NSString*)str
{
    NSRange r;
    while ((r = [str rangeOfString:@"<pre><a style=\"\" name=\"output-line-[0-9]+\">[0-9]+</a>"  options:NSRegularExpressionSearch]).location != NSNotFound) 
    {
        str = [str stringByReplacingCharactersInRange:r withString:@""];
    }
 }

Or you can use NSRegularExpression:

NSMutableString *input = ...

NSError *error;
NSRegularExpression *regex;
regex = [NSRegularExpression regularExpressionWithPattern:@"<pre><a style=\"\" name=\"output-line-[0-9]+\">[0-9]+</a>"
                                                  options:0
                                                    error:&error];
if (error)
{
    NSLog(@"error=%@",error);
    return;
}

[regex replaceMatchesInString:input
                      options:0
                        range:NSMakeRange(0, [input length])
                 withTemplate:@""];

Comments

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.