regular expression to read the string between <title> and </title>
<p>I hope to read the contents between  and  in a html string.</p>

<p>I think it should be in objective-c</p>

<pre><code>@"&lt;title([\\s\\S]*)&lt;/title&gt;"
</code></pre>

<p>below are the codes that rewrited for regular expression</p>

<pre><code>//source of NSStringCategory.h
#import &lt;Foundation/Foundation.h&gt;
#import &lt;regex.h&gt;


@interface NSStringCategory:NSObject
{
    regex_t preg;
}

-(id)initWithPattern:(NSString *)pattern options:(int)options;
-(void)dealloc;

-(BOOL)matchesString:(NSString *)string;
-(NSString *)matchedSubstringOfString:(NSString *)string;
-(NSArray *)capturedSubstringsOfString:(NSString *)string;

+(NSStringCategory *)regexWithPattern:(NSString *)pattern options:(int)options;
+(NSStringCategory *)regexWithPattern:(NSString *)pattern;

+(NSString *)null;

+(void)initialize;

@end


@interface NSString (NSStringCategory)


-(BOOL)matchedByPattern:(NSString *)pattern options:(int)options;

-(BOOL)matchedByPattern:(NSString *)pattern;

-(NSString *)substringMatchedByPattern:(NSString *)pattern options:(int)options;


-(NSString *)substringMatchedByPattern:(NSString *)pattern;


-(NSArray *)substringsCapturedByPattern:(NSString *)pattern options:(int)options;


-(NSArray *)substringsCapturedByPattern:(NSString *)pattern;


-(NSString *)escapedPattern;

@end
</code></pre>

<p>and .m file</p>

<pre><code> #import "NSStringCategory.h"
static NSString *nullstring=nil;

@implementation NSStringCategory

-(id)initWithPattern:(NSString *)pattern options:(int)options
{
    if(self=[super init])
    {
        int err=regcomp(&amp;preg,[pattern UTF8String],options|REG_EXTENDED);
        if(err)
        {
            char errbuf[256];
            regerror(err,&amp;preg,errbuf,sizeof(errbuf));
            [NSException raise:@"CSRegexException"
                        format:@"Could not compile regex \"%@\": %s",pattern,errbuf];
        }
    }
    return self;
}

-(void)dealloc
{
    regfree(&amp;preg);
    [super dealloc];
}

-(BOOL)matchesString:(NSString *)string
{
    if(regexec(&amp;preg,[string UTF8String],0,NULL,0)==0) return YES;
    return NO;
}

-(NSString *)matchedSubstringOfString:(NSString *)string
{
    const char *cstr=[string UTF8String];
    regmatch_t match;
    if(regexec(&amp;preg,cstr,1,&amp;match,0)==0)
    {
        return [[[NSString alloc] initWithBytes:cstr+match.rm_so
                                         length:match.rm_eo-match.rm_so encoding:NSUTF8StringEncoding] autorelease];
    }

    return nil;
}

-(NSArray *)capturedSubstringsOfString:(NSString *)string
{
    const char *cstr=[string UTF8String];
    int num=preg.re_nsub+1;
    regmatch_t *matches=calloc(sizeof(regmatch_t),num);

    if(regexec(&amp;preg,cstr,num,matches,0)==0)
    {
        NSMutableArray *array=[NSMutableArray arrayWithCapacity:num];

        int i;
        for(i=0;i&lt;num;i++)
        {
            NSString *str;

            if(matches[i].rm_so==-1&amp;&amp;matches[i].rm_eo==-1) str=nullstring;
            else str=[[[NSString alloc] initWithBytes:cstr+matches[i].rm_so
                                               length:matches[i].rm_eo-matches[i].rm_so encoding:NSUTF8StringEncoding] autorelease];

            [array addObject:str];
        }

        free(matches);

        return [NSArray arrayWithArray:array];
    }

    free(matches);

    return nil;
}

+(NSStringCategory *)regexWithPattern:(NSString *)pattern options:(int)options
{ return [[[NSStringCategory alloc] initWithPattern:pattern options:options] autorelease]; }

+(NSStringCategory *)regexWithPattern:(NSString *)pattern
{ return [[[NSStringCategory alloc] initWithPattern:pattern options:0] autorelease]; }

+(NSString *)null { return nullstring; }

+(void)initialize
{
    if(!nullstring) nullstring=[[NSString alloc] initWithString:@""];
}

@end

@implementation NSString (NSStringCategory)

-(BOOL)matchedByPattern:(NSString *)pattern options:(int)options
{
    NSStringCategory *re=[NSStringCategory regexWithPattern:pattern options:options|REG_NOSUB];
    return [re matchesString:self];
}

-(BOOL)matchedByPattern:(NSString *)pattern
{ return [self matchedByPattern:pattern options:0]; }

-(NSString *)substringMatchedByPattern:(NSString *)pattern options:(int)options
{
    NSStringCategory *re=[NSStringCategory regexWithPattern:pattern options:options];
    return [re matchedSubstringOfString:self];
}

-(NSString *)substringMatchedByPattern:(NSString *)pattern
{ return [self substringMatchedByPattern:pattern options:0]; }

-(NSArray *)substringsCapturedByPattern:(NSString *)pattern options:(int)options
{
    NSStringCategory *re=[NSStringCategory regexWithPattern:pattern options:options];
    return [re capturedSubstringsOfString:self];
}

-(NSArray *)substringsCapturedByPattern:(NSString *)pattern
{ return [self substringsCapturedByPattern:pattern options:0]; }

-(NSString *)escapedPattern
{
    int len=[self length];
    NSMutableString *escaped=[NSMutableString stringWithCapacity:len];

    for(int i=0;i&lt;len;i++)
    {
        unichar c=[self characterAtIndex:i];
        if(c=='^'||c=='.'||c=='['||c=='$'||c=='('||c==')'
           ||c=='|'||c=='*'||c=='+'||c=='?'||c=='{'||c=='\\') [escaped appendFormat:@"\\%C",c];
        else [escaped appendFormat:@"%C",c];
    }
    return [NSString stringWithString:escaped];
}



@end
</code></pre>

<p>I use the codes below to get the string between "" and ""</p>

<pre><code>NSStringCategory *a=[[NSStringCategory alloc] initWithPattern:@"&lt;title([\s\S]*)&lt;/title&gt;" options:0];//
</code></pre>

<p>Unfortunately [a matchedSubstringOfString:response]] always returns nil</p>

<p>I do not if the regular expression is wrong or any other reason.</p>

<p>Welcome any comment</p>

<p>Thanks</p>

<p>interdev</p>
