-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 ;
}
James_Duh
  • 1,321
  • 11
  • 31
  • 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. – Wiktor Stribiżew Sep 28 '17 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 – James_Duh Sep 28 '17 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. – Wiktor Stribiżew Sep 28 '17 at 20:53
  • 1
    I answered a *very* similar question yesterday: https://stackoverflow.com/a/46455152/3600709 – ctwheels Sep 28 '17 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()`**. – Wiktor Stribiżew Sep 28 '17 at 20:58
  • That's actually a "permitted" character in the link, just remove the space in `[\p{S}\p{P} ]` and add `\s` into `[^\p{C}]` – ctwheels Sep 28 '17 at 21:03
  • @James_Duh A `s.matches("\\S*")` *does not* mach a string with whitespace. See [proof](https://ideone.com/0SOxY0). – Wiktor Stribiżew Sep 28 '17 at 21:10
  • Just put `^(?!.* )\K` at the beginning of your pattern. Edit: actually, you shouldn't even need to reset the match with `\K`. – CAustin Sep 28 '17 at 22:33
  • You removed the space from the pattern I provided. It needs to be `(?!.* )`, not `(?!.*)`. – CAustin Sep 28 '17 at 22:42
  • @CAustin Well this works here is syntax of what I added ^(?!.*[\s]) Thanks a ton post as an answer Do you mind explaining ?! this I get not equal and the ^ is look forward but why only one set of .* at the front and not at the end ? ANY ONE READING THIS HERE IS THE ANSWER TO PREVENT SPACE IN THE with Regex on a string in an edit text – James_Duh Sep 28 '17 at 22:48
  • `\s` will also work in this situation, but you should note that `\s` matches *all* whitespace characters such as newlines and tabs, not just normal spaces. It's generally a bad idea to use it when you're just looking for spaces. The `?!` does not mean "not equal" in regex; it's the character sequence for negative lookahead. It indicates that the match should fail if the characters inside those parentheses are found next. `^` merely anchors the beginning of the string. https://regex101.com/r/Yqaajy/9 – CAustin Sep 28 '17 at 22:54
  • @James_Duh you only need to match one space for it to be invalid, therefore you can match any character with `.*` and whitespace characters with `\s` (btw `[\s]` and `\s` are equivalent so you can drop the brackets). So if it matches any character any number of times and space, then it means a space must exist somewhere in the string, therefore making it invalid – ctwheels Sep 28 '17 at 22:54

1 Answers1

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!@$%&*?]+

CAustin
  • 4,525
  • 13
  • 25
  • 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 Sep 29 '17 at 04:07
  • @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 – Vector Sep 29 '17 at 04:13