Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 714971
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T05:08:05+00:00 2026-05-14T05:08:05+00:00

I hope to read the contents between and in a html string. I think

  • 0

I hope to read the contents between and in a html string.

I think it should be in objective-c

@"<title([\\s\\S]*)</title>"

below are the codes that rewrited for regular expression

//source of NSStringCategory.h
#import <Foundation/Foundation.h>
#import <regex.h>


@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

and .m file

 #import "NSStringCategory.h"
static NSString *nullstring=nil;

@implementation NSStringCategory

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

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

-(BOOL)matchesString:(NSString *)string
{
    if(regexec(&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(&preg,cstr,1,&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(&preg,cstr,num,matches,0)==0)
    {
        NSMutableArray *array=[NSMutableArray arrayWithCapacity:num];

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

            if(matches[i].rm_so==-1&&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<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

I use the codes below to get the string between “” and “”

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

Unfortunately [a matchedSubstringOfString:response]] always returns nil

I do not if the regular expression is wrong or any other reason.

Welcome any comment

Thanks

interdev

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T05:08:05+00:00Added an answer on May 14, 2026 at 5:08 am

    (Preliminary warning: you can’t parse HTML correctly with Regex.)


    You are using regex.h, which provides POSIX regular expression (ERE in your case). They do not support all of the PCRE syntax such as \s and \S (and [\s\S] is useless anyway — it matches anything).

    Probably you should use

    initWithPattern:@"<title[^>]*>([^<]*)</title>" options:REG_ICASE
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.