0

I've some regular expressions for the form fields validation.

I've an unit test to define the expected result

NSArray *suiteWebs = [NSArray arrayWithObjects:
                      @"http://webapp.stackoverflow.net",
                      @"http://webapp.stackoverflow.net/info.php",
                      @"http://www.stackoverflow.net",
                      @"http://www.stackoverflow.net/",
                      @"https://webapp.stackoverflow.net",
                      @"https://webapp.stackoverflow.net/info.php",
                      @"https://www.stackoverflow.net",
                      @"https://www.stackoverflow.net/"
                      @"webapp.stackoverflow.net",
                      @"webapp.stackoverflow.net/info.php",
                      @"www.stackoverflow.net",
                      @"www.stackoverflow.net/",
                      @"www.stack-overflow.com",
                      @"www.stackoverflow_.com",
                      @"www.stackover_flow.com",
                      nil];

NSArray *falseSuiteWebs = [NSArray arrayWithObjects:
                           @"ftp://webapp.stackoverflow.net",
                           @"http:/www.stackoverflow.net",
                           @"ftps://webapp.stackoverflow.net",
                           @"https:/www.stackoverflow.net",
                           nil];

for (NSString *web in suiteWebs) {
    NSLog(@"Validating web %@", web);
    STAssertTrue([TSAddEntityForm validateWeb:web withPatter:currentRegex], [NSString stringWithFormat:@"currentRegex web %@", web]);
}

for (NSString *web in falseSuiteWebs) {
    NSLog(@"Validating web %@", web);
    STAssertFalse([TSAddEntityForm validateWeb:web withPatter:currentRegex], [NSString stringWithFormat:@"currentRegex web %@", web]);
}

My actual regular expression is the next one:

NSString *webRegex4 = @"((http|https)://){0,1}((\\w)*|([0-9]*)|([\\-|_])*)+([\\.|/]((\\w)*|([0-9]*)|([\\-|_])*))+";

My problem are with the domains with - my regular expression don't validate it. For example the url www.stack-overflow.com is rejected

Any suggestions?

Thank you

0

2 Answers 2

2

May be this regexp would be better in your case (it's not ideal, but works for above suitable and bad samples):

(http(s)?://)?[\w-]+(\.[\w-]+)*\.\w{2,6}[/\w.-]*

It may begin from http:// or https://,

[\w-]+(\.[\w-]+)*\.\w{2,6} - describes domain

[/\w.-]* - folders and documents

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you!! It's exactly what we need.
1

In general, complex regular expressions are fool's gold. Use multiple passes with multiple regular expressions. Validate components of the URLs independently.

Complex regular expressions can be very powerful, but can also paint you into a fragile corner with something as open ended as URLs.

Also, if you're using Objective-C, it is easy to break things down with some of the facilities provided by NSURL. NSURL will also give you a good idea of what components of a URL you should look at. By using NSURL methods to extract components of the URLs, you can apply your regular expressions more carefully to each component.

CFURL is equally powerful.

2 Comments

Yep, it's a good point. But I prefer to find the complex regular expression in order to have the same validation pattern in the different platforms we have. Thanks!
You can still break it into components and handle them on multiple platforms. This seems more like a regular expression question than a Objective-C question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.