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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T01:22:25+00:00 2026-05-30T01:22:25+00:00

I have this regex working when I test it in PHP but it doesn’t

  • 0

I have this regex working when I test it in PHP but it doesn’t work in Objective C:

(?:www\.)?((?!-)[a-zA-Z0-9-]{2,63}(?<!-))\.?((?:[a-zA-Z0-9]{2,})?(?:\.[a-zA-Z0-9]{2,})?)

I tried escaping the escape characters but that doesn’t help either. Should I escape any other character?

This is my code in Objective C:

NSMutableString *searchedString = [NSMutableString stringWithString:@"domain-name.tld.tld2"];
NSError* error = nil;

NSRegularExpression* regex = [NSRegularExpression regularExpressionWithPattern:@"(?:www\\.)?((?!-)[a-zA-Z0-9-]{2,63}(?<!-))\\.?((?:[a-zA-Z0-9]{2,})?(?:\\.[a-zA-Z0-9]{2,})?)" options:0 error:&error];
NSArray* matches = [regex matchesInString:searchedString options:0 range:NSMakeRange(0, [searchedString length])];
for ( NSTextCheckingResult* match in matches )
{
    NSString* matchText = [searchedString substringWithRange:[match range]];
    NSLog(@"match: %@", matchText);
}

— UPDATE —

This regex returns (in PHP) the array with values "domain-name" and "tld.tld2" but in Objective C i get only one value: "domain-name.tld.tld2"

— UPDATE 2 —

This regex extracts ‘domain name’ and ‘TLD’ from the string:

  • example.com = (example, com)
  • example.co.uk = (example, co.uk)
  • -test-example.co.u = (test-example, co)
  • -test-example.co.uk- = (test-example, co.uk)
  • -test-example.co.u-k = (test-example, co)
  • -test-example.co-m = (test-example)
  • -test-example-.co.uk = (test-example)

it takes the valid domain name (not starting or ending with ‘-‘ and between 2 and 63 characters long), and up to two parts of a TLD if the parts are valid (at least two characters long containing only letters and numbers)

  • 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-30T01:22:27+00:00Added an answer on May 30, 2026 at 1:22 am

    A NSTextCheckingResult has multiple items obtained by indexing into it.

    [match rangeAtIndex:0]; is the full match.
    [match rangeAtIndex:1]; (if it exists) is the first capture group match.
    etc.

    You can use something like this:

    NSString *searchedString = @"domain-name.tld.tld2";
    NSRange   searchedRange = NSMakeRange(0, [searchedString length]);
    NSString *pattern = @"(?:www\\.)?((?!-)[a-zA-Z0-9-]{2,63}(?<!-))\\.?((?:[a-zA-Z0-9]{2,})?(?:\\.[a-zA-Z0-9]{2,})?)";
    NSError  *error = nil;
    
    NSRegularExpression* regex = [NSRegularExpression regularExpressionWithPattern: pattern options:0 error:&error];
    NSArray* matches = [regex matchesInString:searchedString options:0 range: searchedRange];
    for (NSTextCheckingResult* match in matches) {
        NSString* matchText = [searchedString substringWithRange:[match range]];
        NSLog(@"match: %@", matchText);
        NSRange group1 = [match rangeAtIndex:1];
        NSRange group2 = [match rangeAtIndex:2];
        NSLog(@"group1: %@", [searchedString substringWithRange:group1]);
        NSLog(@"group2: %@", [searchedString substringWithRange:group2]);
    }
    

    NSLog output:

    match: domain-name.tld.tld2
    domain-name
    tld.tld2

    Do test that the match ranges are valid.

    More simply in this case:

    NSString *searchedString = @"domain-name.tld.tld2";
    NSRange   searchedRange = NSMakeRange(0, [searchedString length]);
    NSString *pattern = @"(?:www\\.)?((?!-)[a-zA-Z0-9-]{2,63}(?<!-))\\.?((?:[a-zA-Z0-9]{2,})?(?:\\.[a-zA-Z0-9]{2,})?)";
    NSError  *error = nil;
    
    NSRegularExpression* regex = [NSRegularExpression regularExpressionWithPattern:pattern options:0 error:&error];
    NSTextCheckingResult *match = [regex firstMatchInString:searchedString options:0 range: searchedRange];
    NSLog(@"group1: %@", [searchedString substringWithRange:[match rangeAtIndex:1]]);
    NSLog(@"group2: %@", [searchedString substringWithRange:[match rangeAtIndex:2]]);
    

    Swift 3.0:

    let searchedString = "domain-name.tld.tld2"
    let nsSearchedString = searchedString as NSString
    let searchedRange = NSMakeRange(0, searchedString.characters.count)
    let pattern = "(?:www\\.)?((?!-)[a-zA-Z0-9-]{2,63}(?<!-))\\.?((?:[a-zA-Z0-9]{2,})?(?:\\.[a-zA-Z0-9]{2,})?)"
    
    do {
        let regex = try NSRegularExpression(pattern:pattern, options: [])
        let matches = regex.matches(in:searchedString, options:[], range:searchedRange)
        for match in matches {
            let matchText = nsSearchedString.substring(with:match.range);
            print("match: \(matchText)");
    
            let group1 : NSRange = match.rangeAt(1)
            let matchText1 = nsSearchedString.substring(with: group1)
            print("matchText1: \(matchText1)")
    
            let group2 = match.rangeAt(2)
            let matchText2 = nsSearchedString.substring(with: group2)
            print("matchText2: \(matchText2)")
        }
    } catch let error as NSError {
        print(error.localizedDescription)
    }
    

    print output:

    match: domain-name.tld.tld2
    matchText1: domain-name
    matchText2: tld.tld2

    More simply in this case:

    do {
        let regex = try NSRegularExpression(pattern:pattern, options: [])
        let match = regex.firstMatch(in:searchedString, options:[], range:searchedRange)
    
        let matchText1 = nsSearchedString.substring(with: match!.rangeAt(1))
        print("matchText1: \(matchText1)")
    
        let matchText2 = nsSearchedString.substring(with: match!.rangeAt(2))
        print("matchText2: \(matchText2)")
    
    } catch let error as NSError {
        print(error.localizedDescription)
    }
    

    print output:

    matchText1: domain-name
    matchText2: tld.tld2

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

Sidebar

Related Questions

I have this regex working but now need to allow numbers without the decimal
I have this regex to test for telephone # that should be a toll
I'm really new to Regex and working hard, but this has gone beyond simple
I have been working on this regex: {link=([^|{}]+)\||([^|{}]+)\||([^|{}]+)} I wish to capture any non-pipe
I have this simple cgi script working just fine but I want to add
I have this regex I built and tested in regex buddy. _ [ 0-9]{10}+
I have this regex thanks to another wonderful StackOverflow user /(?:-\d+)*/g I want it
I have this regex: private static final String SPACE_PATH_REGEX =[a-z|A-Z|0-9|\\/|\\-|\\_|\\+]+; I check if my
So I have this regex: (^(\s+)?(?P<NAME>(\w)(\d{7}))((01f\.foo)|(\.bar|\.goo\.moo\.roo))$|(^(\s+)?(?P<NAME2>R1_\d{6}_\d{6}_)((01f\.foo)|(\.bar|\.goo\.moo\.roo))$)) Now if I try and do a match
Ok so I have this regex that I created and it works fine in

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.