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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T05:32:52+00:00 2026-06-04T05:32:52+00:00

I use Regex for checking a password. But it is not working as intended.

  • 0

I use Regex for checking a password. But it is not working as intended.
I use some Unit-tests to check the regex.

@implementation NSString (regexMatch)

- (BOOL)matchesRegex:(NSString *)regexPattern
{
    NSError *error = nil;

    NSRegularExpression* regex = [NSRegularExpression
                                  regularExpressionWithPattern:regexPattern 
                                  options:NSRegularExpressionSearch
                                  error:&error];

    if(error) 
    {
        NSLog(@"Error on maches regex: %@ on %@: %@", regex, self, error);
        return NO;
    }

    NSUInteger maches = [regex numberOfMatchesInString:self 
                                               options:NSRegularExpressionSearch 
                                                 range:NSMakeRange(0, [self length])];

    return (maches > 0);
}

@end

The Regex:

const static NSString *REGEX_VALID_PASS = @"(^((?=.*\\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[-_,.@#$%'\"]).{6,20})$)";

Unit tests:

//Password 
// At least:
//  - between 6 and 20 chars
//  - a capital character
//  - a small character
//  - a digit
//  - a special char: - _ , . @ # $ % ´ '
STAssertTrue([@"Pass1-" matchesRegex:REGEX_VALID_PASS], @"Password 1 failed");

STAssertTrue([@"aPrettyLongPassw0rd@" matchesRegex:REGEX_VALID_PASS], @"Password 2 failed");

STAssertTrue([@"Pw987321-_,.@#$%'`" matchesRegex:REGEX_VALID_PASS], @"Password 3 failed");

//Returns TRUE ???
STAssertFalse([@"password1@" matchesRegex:REGEX_VALID_PASS], @"Wrong password 4 works (No capital letter)");

//Returns TRUE ???
STAssertFalse([@"PASSWORD2#" matchesRegex:REGEX_VALID_PASS], @"Wrong password 5 works (No small letter)");

STAssertFalse([@"Password8" matchesRegex:REGEX_VALID_PASS], @"Wrong password 6 works (No special letter)");

STAssertFalse([@"Password$" matchesRegex:REGEX_VALID_PASS], @"Wrong password 7 works (No digit letter)");

STAssertFalse([@"PrettyLongPasswordIsWrong123#" matchesRegex:REGEX_VALID_PASS], @"Wrong password 8 works (> 20)");

STAssertFalse([@"Pw@3" matchesRegex:REGEX_VALID_PASS], @"Wrong password 9 works (< 6)");

Password ‘password1@’ and ‘PASSWORD2#’ are returning TRUE but there are not correct.

Errors:

file://localhost/Users/user/app/AppTest.m: error: testRegex (AppTest) failed: "[@"password1@" matchesRegex:REGEX_VALID_PASS]" should be false. Wrong password 4 works (No capital letter)

and


file://localhost/Users/user/app/AppTest.m: error: testRegex (AppTest) failed: "[@"PASSWORD2#" matchesRegex:REGEX_VALID_PASS]" should be false. Wrong password 5 works (No small letter)
  • 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-04T05:32:54+00:00Added an answer on June 4, 2026 at 5:32 am

    You specified NSRegularExpressionCaseInsensitive option, so it makes sense that the expression does not differentiate between upper and lower case letters.

    With the NSRegularExpressionCaseInsensitive option specified, (?=.*[a-z]) will match any letter, including capital ones; similarly, (?=.*[A-Z]) will match lowercase letters as well.

    Your failed unit tests expect a different behavior: both of them are checking that the password contains upper and lower case letters, while the input contains letters in the same case.

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

Sidebar

Related Questions

I want to use regex to replace src html attributes. The HTML is not
I don't know if I can use regex for this, but I want to
Ok I know everyone is going to tell me not to use RegEx for
In PHP, I use this regex for checking mails: $rexMail = /^[a-z0-9\._]+@{1}[a-z0-9-_]+\.{1}[a-z]{2,4}\.?[a-z]{0,2}$/i; In most
i tried to use Regex in c# , but the following code throws an
I have a regex pattern for URL's that I use to check for links
I use REGEX. How can I find the ] symbol? I tried [\]] but
trying to use regex to replace any white space with &nbsp;, inside of example
If i want to use regex to find either < or > within a
I'm trying to use regex as the conditional in a Ruby (1.9.2) if statement

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.