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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T06:34:12+00:00 2026-05-15T06:34:12+00:00

NSString *myText = @mary had a little lamb; NSString *regexString = @mary(.*?)little; for(NSString *match

  • 0
NSString *myText = @"mary had a little lamb";
NSString *regexString = @"mary(.*?)little";

for(NSString *match in [myText captureComponentsMatchedByRegex:regexString]){

NSLog(@"%@",match);

}

This will output to the console two things:

1) “mary had a little”
2) “had a”

What I want is just the 2nd bit of information “had a”. Is there is a way of matching a string and returning just the inner part?

I’m fairly new to Objective C, this feels a rather trivial question yet I can’t find a less messy way of doing this than incrementing an integer in the for loop and on the second iteration storing the “had a” in an NSString.

  • 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-15T06:34:12+00:00Added an answer on May 15, 2026 at 6:34 am

    With regular expressions, it’s standard that the first match returned is the whole matched string (“mary had a little”), then the next items are the captured groups (“had a”).

    -captureComponentsMatchedByRegex: returns a NSArray of the matches. So, if you want the second item, or the first captured group:

    NSString *match = [[myText captureComponentsMatchedByRegex:regexString] objectAtIndex:1]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

NSString *searchString = @Lyngbø; NSLog(%@,[searchString stringByAddingPercentEscapeUsingEncoding:NSUTF8StringEncoding]); This gives me : Lyng%C3%B8 <script type=text/javascript> document.write(escape(Lyngbø));
NSString *soundURL = [NSString stringWithFormat:@http://translate.google.com/translate_tts?q=%@&tl=%@,strTextOfFilter,strCodeOfLanguage]; NSLog(@URL : %@,soundURL); this show: http://translate.google.com/translate_tts?q=%E4%BD%A0%E5%A5%BD&tl=zh-TW copy url to
NSString *test = @d'escape; NSLog(@%@, [test stringByReplacingOccurrencesOfString:@' withString:@\']); prints me this 2010-10-25 15:10:54.833 MyApp[7136:207]
NSString *x = @12345; NSInteger nsint = [x integerValue]; NSLog(@%x, nsint); Prints 3039. intValue
In MyClass.m, I've defined - (void) myTest: (NSString *) withAString{ NSLog(@hi, %@, withAString); }
NSString *notAllocatedString = @This string was not allocated.; NSString *allocatedString = [[NSString alloc] initWithFormat:@This
surely, there is a shorter way to do this? NSString *filePathA = [[NSBundle mainBundle]
-(NSString)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component when i try to implement this method, i get the
I have an NSString variable called myText . How to watch [myText length] in
NSString *yValue; yValue= [[NSString alloc] initWithFormat:@%1.2f, imageView.center.y]; [label setText:yValue]; I am trying to display

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.