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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T00:37:58+00:00 2026-06-18T00:37:58+00:00

Consider this text: Paragraph 1: Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed

  • 0

Consider this text:

Paragraph 1: Lorem ipsum dolor sit amet, consectetur adipisicing elit,
sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut
enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi
ut aliquip ex ea commodo consequat. 

Paragraph 2 Lorem ipsum dolor sit amet, consectetur adipisicing elit,
sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut
enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi
ut aliquip ex ea commodo consequat.







Paragraph 3 Lorem ipsum dolor sit amet, consectetur adipisicing elit,
sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut
enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi
ut aliquip ex ea commodo consequat.

In ObjC, when reading the above text, there are two \n\n line spaces between paragraph1 and paragraph2. But there are more than 3 line spaces \n\n\n\n between paragraph2 and paragraph3.

I wanted to have an NSRegularExpression pattern that would read and return those paragraphs completely disregarding the number of linespaces.

NSString *pattern = @"\n(*\n)\n";

NSRegularExpression* regex1 = [[NSRegularExpression alloc] initWithPattern:pattern options:NSRegularExpressionCaseInsensitive error:nil];

NSArray *array = [regex1 matchesInString:p options:0 range:NSMakeRange(0, [p length])];
for(NSTextCheckingResult *tcr in array){
    NSTextCheckingResult *tcr = [regex1 firstMatchInString:p options:0 range:NSMakeRange(0, p.length)];
    NSRange matchRange = [tcr rangeAtIndex:1];
    NSString *amatch = [p substringWithRange:matchRange];
    NSLog(@"Found string: %@", amatch);
}

I’m new to NSRegularExpression, any reference to a better tutorial would be great. In this case and, is this the right way to go about it in the above question.

  • 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-06-18T00:37:58+00:00Added an answer on June 18, 2026 at 12:37 am

    The following does the job. I also used the enumerateMatchesInString to find matches.

    NSString *pattern = @"(\\A|\\n\\s*\\n)(.*?\\S[\\s\\S]*?\\S)(?=(\\Z|\\s*\\n\\s*\\n))";
    NSRegularExpression* regex = [[NSRegularExpression alloc] initWithPattern:pattern
                                                                      options:NSRegularExpressionCaseInsensitive
                                                                        error:&error];
    
    [regex enumerateMatchesInString:input
                            options:0
                              range:NSMakeRange(0, [input length])
                         usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop) {
                             NSString *match = [input substringWithRange:[result rangeAtIndex:2]];
                             NSLog(@"match = '%@'", match);
                         }];
    

    This returns not only the strings between two newline characters (ignoring any extra whitespace between the returns), but also the first one (i.e. between the beginning of the string and the first sequence of two newlines) and the last one (i.e. between the last sequence of two newlines and the end of the string.

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

Sidebar

Related Questions

Consider the following HTML: <p>This is a potentially large paragraph of text, which <span>may
Consider this text paragraph Conservation groups call the 20-year ban a crucial protection for
Consider this code: const char* someFun() { // ... some stuff return Some text!!
Consider this: my $var = ${SOME_VAR} my $string In this text ${SOME_VAR} will be
consider this function: jQuery.fn.MyFunction = function (event) { alert(this.text()); } I want to call
Consider this simplest piece of code: using System; using System.Collections.Generic; using System.Linq; using System.Text;
Consider this piece of code: using System; using System.Collections.Generic; using System.Linq; using System.Text; using
Consider this code below: <a href="javascript:void(-1)" id="a1">a1</a> <a href="javascript:void(-1)" id="a2">a2</a> <script type="text/javascript"> var buttons
Consider this code: <script type=text/javascript> if ('mySuperProperty' in window) { alert(window['mySuperProperty']); } var mySuperProperty
Let's consider this sample example text file: Level: 1 Level: 1 Level: 2 Level:

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.