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 6078719
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T10:51:54+00:00 2026-05-23T10:51:54+00:00

How do I search through a string for a particular substring and find the

  • 0

How do I search through a string for a particular substring and find the positions of each matching item.

For example,

NSString *theText = @"name - dave, age - 30, favNum - 7, name - peter, age - 8, favNum - 10";

In this example, I am trying to find all the ages and replace them with 50. What is the best way of doing so?

  • 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-23T10:51:55+00:00Added an answer on May 23, 2026 at 10:51 am

    NSRegularExpression is available only since iOS 4.0 and the string should have predictable regex for this to work as one would guess.

    NSString *theText = @"name - dave, age - 30, favNum - 7, name - peter, age - 8, favNum - 10";
    NSRegularExpression * regularExpression = [NSRegularExpression regularExpressionWithPattern:@"(?<= age - )\\d+"
                                                                                        options:0
                                                                                          error:nil];
        
    NSString * alteredText = [regularExpression stringByReplacingMatchesInString:theText
                                                                         options:0
                                                                           range:NSMakeRange(0, [theText length])
                                                                    withTemplate:@"50"];
    
    NSLog(@"%@", alteredText);
    

    The above code works. You will have to see if it fits your HTML use case.

    Getting the ages

    You will use the enumerateMatchesInString:options:range:usingBlock: method to enumerate through all the matches. You will get a NSTextCheckingResult object for each iteration which will hold the range information about the match. So you will need to do this before you replace the string. You can get the ages in an array doing,

    NSMutableArray * ages = [NSMutableArray array];
    [regularExpression enumerateMatchesInString:theText
                                        options:0
                                          range:NSMakeRange(0, [theText length])
                                     usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop){
                                         [ages addObject:[NSNumber numberWithInt:[[theText substringWithRange:result.range] intValue]]];
                                     }];
    
    NSLog(@"%@", ages);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently using regular expressions to search through RSS feeds to find if certain
When i search files through a string in my local drives it shows the
I'm trying to write a perl script to search through a text file, find
So I want to search through a string to see if it contains the
I would like to search through all of my procedures packages and functions for
I have search through the web to figure this out but no luck. I
I have to search through a list and replace all occurrences of one element
Is there a tool to automatically search through my site and test all the
I would like my python script to search through a directory in SVN, locate
I am looking for a way to search through every users profile using the

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.