-3

I have see numerous suggestions for regex to find whitespace in a string none of which have worked so far. Yes the concept of looping through the string with a for next loop will work. I would really like to learn how to do this with regex and Pattern Matcher ! My question is what and where do I need to add to my regex string so it will return FALSE? code below I have added numerous incarnations of (\\s) to no avail. I do not want to remove the whitespace.

I tested the code suggested as a duplicate and it does not work see the link suggested in the comments

String tstr = "^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)(?=.*[$@$!%*?&])[A-Za-z\\d$@$!%*?&]";

String astr = etPW.getText().toString().trim();
Pattern regex = Pattern.compile(tstr);
Matcher regexMatcher = regex.matcher(astr);

boolean foundMatch = regexMatcher.find();

if(foundMatch == false){
    Toast.makeText( MainActivity.this, "Password must have one Numeric Value\n"
      + "\nOne Upper & Lower Case Letters\n"
      + "\nOne Special Character $ @ ! % * ? &", Toast.LENGTH_LONG ).show();
    //etPW.setText("");
    //etCPW.setText("");
    // Two lines of code above are optional
    // Also by design these fields can be set to input type Password in the XML file
    etPW.requestFocus();

    return ;
}
12
  • What are your password requirements? Why do you repeat $ twice in the character classes? I have seen that many times - why copy some regex without really understanding what it is doing? And it seems you copied a part of it. Commented Sep 28, 2017 at 20:46
  • @WiktorStribiżew I copied this code from a regex tester site or so so while I will agree I may not understand the code completely. Requirements are NO white space include one UPPER CASE and one lower case and numbers are permitted AND old school one Special Character. I will test removing the $ twice in the character classes OK Dupes have been removed THANKS Commented Sep 28, 2017 at 20:51
  • The double $ is not doing any harm, it is just bad style. You might try "(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)(?=.*[$@$!%*?&])\\S*" but use it with regexMatcher.matches(). Please update the question to include actual requirements. Commented Sep 28, 2017 at 20:53
  • 1
    I answered a very similar question yesterday: stackoverflow.com/a/46455152/3600709 Commented Sep 28, 2017 at 20:54
  • Can you copy/paste what you see on the screen as it is? I wrote "(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)(?=.*[$@!%*?&])\\S*" and use with regexMatcher.matches(). Commented Sep 28, 2017 at 20:58

1 Answer 1

2

You can use negative lookahead to check for spaces:

^(?!.* )

^ - Start matching at the beginning of the string.

(?! - Begin a negative lookahead group (the pattern inside the parentheses must not come next.

.* - Any non-newline character any number of times followed by a space.

) - Close the negative lookahead group.

Combined with the full regex pattern (also cleaned up a bit to remove redundancy):

^(?!.* )(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)(?=.*[!@$%&*?])[A-Za-z\\d!@$%&*?]+

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

2 Comments

I am not sure why but the first grouping need to be formatted with the \\s as in this example ^(?!.*\\s) So any looking at this long conversation in Android Studio that is how it is to WORK Thanks to everyone who contributed and offered assistance !
@James_Duh thanks for the comment I tried as posted and it would not work Regex seems to have its own ideas depending on the flavor you are working with

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.