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

  • SEARCH
  • Home
  • 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 4006110
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T08:28:31+00:00 2026-05-20T08:28:31+00:00

My iphone app uses regular expressions (with NSRegularExpression) to perform calculations over a very

  • 0

My iphone app uses regular expressions (with NSRegularExpression) to perform calculations over a very large number of strings (in the 1000s). This of course takes a lot of time. What are some strategies to speed up the regular expressions? I looked into using blocks, but I don’t think it will do any good — they seem to mostly represent lambda functionality (i.e., equivalent to lisp) and are used on the Mac with multiple cores. Obviously, the current iPhone doesn’t have multiple cores.

Here’s my code:

NSString *replaceRegexPattern = @"([\\(|\\[].*?[\\)|\\]])|(^to )";
NSRegularExpression *replaceRegex = [[NSRegularExpression regularExpressionWithPattern:replaceRegexPattern
                                                                              options:NSRegularExpressionCaseInsensitive
                                                                               error:nil] retain];
NSArray *myArray = <some data>;
NSString *myString, *compareValue;
for (i = 0; i < [myArray count]; i++) {
    myString = [myArray objectAtIndex:i];
    compareValue = [replaceRegex stringByReplacingMatchesInString:myString
                                                          options:0
                                                            range:NSMakeRange(0, [myString length])
                                                     withTemplate:@""];
    // do things with compareValue

}

To answer the question below, my goal in this code is to remove any text in my string which either is enclosed in parentheses, or which begins with “to “. Here are some examples:

  • Hello (Goodbye) –> Hello
  • Hello (Goodbye [n]) –> Hello
  • To Say –> Say
  • To Say (pf) –> Say
  • 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-20T08:28:31+00:00Added an answer on May 20, 2026 at 8:28 am

    Since I don’t know what exactly you’re trying to do, it’s hard to give well-founded advice, but it looks like your regex could be improved a little.

    Are you really trying to match strings like (foo), [bar], and |baz|? You don’t need the | alternator inside character classes, so unless you want to match the third example here, drop the |s.

    Then, since you’re expecting strings like (foo [bar] baz), you need to separate the two kinds of parentheses, and you can also speed up your regex a bit:

    @"^to |\\([^)]*\\)|\\[[^\\]]*\\]"
    

    This checks for to at the start of the string first, then goes looking for an opening paren/bracket, anything except closing parens/brackets, and a closing paren/bracket. This needs less backtracking so it’s probably a bit faster.

    You won’t be able to handle nested parentheses/brackets of the same kind ((foo (bar) baz)) with a single regex because that’s not regular anymore – unless you run the regex replace operation several times, once for each level of nesting. So the above example will be removed if you run the regex replace twice.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing an iPhone app which uses GLSL shaders to perform transformations on textures,
My iPhone app uses SHA1 for password security over an instant messaging protocol. Will
I'm porting an iPhone app that uses this tutorial into a Mac app. I've
I'm getting ready to finally deploy my first iPhone app. The app uses SSL
My iPhone app generally uses under 5MB of living memory and even in the
I have an iPhone app that uses the json-framework . I moved some of
I'm developing an iPhone app that uses the network. The iPhone communicate with my
I'm developing an iPhone app that uses the network. The iPhone communicate with my
I have written an iPhone app that uses the iPhone's relative GPS location. I
I'm making an iphone app that uses NSURLConnection to download some data from 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.